This repository can easily make a captcha image for you and take control of checking its accuracy.
- Random color with each reload
- Random word types (letter&number, just letter , just number) depends on your luck ^_~
- Random text color (black or white)
- Definitely PHP >= v7.4 ;)
- GD library
- Add Captcha.php and captcha_add.php to your current directory.
- include Captcha.php to your main file
- If GD library is not already active for you, just uncomment
;extension=php_gd2.dll
By removing;
behind it fromphp.ini
. - Follow the steps below
According to demo.php, we assume you have a form in the page:
<form class="mx-auto" method="post">
<div class="form-group">
<label for="exampleInputEmail1">Email address</label>
<input type="email" class="form-control" name="email" placeholder="Enter email">
</div>
<div class="form-group">
<label for="exampleInputPassword1">Password</label>
<input type="password" class="form-control" name="password" placeholder="Password">
</div>
<div class="form-group ">
<label for="exampleInputPassword1">Enter the Captcha</label>
<div class="d-flex">
<input type="text" class="form-control mr-2" name="captcha" placeholder="captcha">
<img src="captcha_add.php" alt="">
</div>
</div>
<button type="submit" class="btn btn-primary" name="submit">Submit</button>
</form>
As you see, I put an image tag with captcha_add.php src where the image is output.
You can have something like this:
- Image width
- Image height
- String font size (optional)
- String length (optional)
- String font family (optional)
You can set these options in captcha_add.php e.g.
$captcha = new Captcha();
$captcha->captchaControl(200,50,null,5,"Sriracha-Regular.ttf");
If you don't want to set a parameter leave it with null value and let it be the default one.
About font file that is the last parameter, you have two choices.
- URL (As the default value) e.g. https://fonts.gstatic.com/s/acme/v17/RrQfboBx-C5_bx0.ttf
- A font file name in the current directory
When you want to check the accuracy of the entered value you can use a static method called checkCaptcha. e.g.
<?php
if (isset($_POST['submit'])){
if (Captcha::checkCaptcha($_POST['captcha'])){
echo "Correct value";
}else{
echo "Wrong value entered!";
}
}
?>