FreeKB - PHP Create a captcha
PHP - Create a captcha

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
  }
?>

 



Add a Comment




We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.




Please enter b2ddb in the box below so that we can be sure you are a human.




Comments

Web design by yours truely - me, myself, and I   |   jeremy.canfield@freekb.net   |