PHP - Create a captcha

by
Jeremy Canfield |
Updated: March 11 2020
| PHP articles
Let's first look at creating a really simple captcha, where will will generate a random string of characters.
<?php
 $system_captcha = substr( md5(rand()), 0, 5);
?>
This will produce a random string, such as a5ecf. We can then create a form, where we will pass both the system captcha and user captcha to form_data.php.
<form method="post" action="form_data.php">
 <input type="hidden" name="system_captcha" value="<?php echo $system_captcha; ?>">
 <input type="text" name="user_captcha">
</form>Â Â Â
When the form data is being processed, the user supplied captcha can be compared to the system captcha.
<?php
$user_captcha = $_POST["user_captcha"];
$system_captcha = $_POST["system_captcha"];
if ($user_captcha == $system_captcha) {
//Success markup
}
else {
//Failure markup
}
?>
Did you find this article helpful?
If so, consider buying me a coffee over at