PHPCaptcha is a PHP library for easy Implementations of captchas.
A captcha is a completely automated public Turing test to tell computers and humans apart.
To place the captcha on your website you need to do the following steps:
- Import PHPCaptcha.php into the namespace (this has to be before initiating the object)
include 'PHPCapcha.php';
- Create a PHPCaptcha object with the path to the folder where the captcha images are stored as argument
$captcha = new PHPCaptcha('img/');
- (Set the width of the image in px)
$captcha->setWidth(200);
This is only necessary if you want to change the with of the image which is set to 300px by default.
- Add the verify() function to your existing code
$error = !$captcha->verify($_POST['input'], $_POST['hash']);
- Add/Remove the verify button of the captcha:
Normaly the captcha is used below an input form, so there should already be a button to send of the form.
If you still want to display the verify button add this line:
$captcha->displayButton(true);
with this button you can just use this code to verify the input:
if(isset($_POST['verify'])){
if($captcha->verify($_POST['input'], $_POST['hash']))
echo "true";
else
echo "false";
}
- To display the captcha use the show() function in the place you want it to be displayed
$captcha->show();
There is a stylesheet which only contains some basic styles. Feel free to add your own styles in there :)
To generate the images, just run create.py with the number of images you want to generate as argument.
(If you have not added the python lib Pillow just run install.py or get it from the official page.)
These images will be saved in the img/ folder in the same directory as the py folder. The file name of the images is the text displayed on them hashed with sha256
If you do not want to generate your own images, there are 50 sample images in the img folder for you to use.
Just for the Captcha you only need PHP 8.0 running
If you want to generate your own captcha images you also need python 3.4 or higher Additionaly you need pip in order to install Pillow