Home > php > Image Captcha PHP

Image Captcha PHP

In these days, one of my clients complaint me that he received a lot of spams from reservation form which I created without preventing spams. That’s why I need to put image captcha on it with PHP. For this, I don’t want to use complicated coding, and want to use simple way. That’s why I got suitable solutions from this website.

http://www.phpjabbers.com/captcha-image-verification-php19.html

<?php
session_start();
$text = rand(10000,99999);
$_SESSION["vercode"] = $text;
$height = 25;
$width = 65;

$image_p = imagecreate($width, $height);
$black = imagecolorallocate($image_p, 0, 0, 0);
$white = imagecolorallocate($image_p, 255, 255, 255);
$font_size = 14;

imagestring($image_p, $font_size, 5, 5, $text, $white);
imagejpeg($image_p, null, 80);
?>

Save this code in a file called captcha.php. Next we need to create our web form like register.html or reservation.html.

<form action=”submit.php” method=”post”>
Comment: <textarea name=”coment”></textarea><br>
Enter Code <img src=”captcha.php”><input type=”text” name=”vercode” /><br>
<input type=”submit” name=”Submit” value=”Submit” />
</form>

All we have to do now is to make the submit.php script which will check if the verification code you enter matches the one that has been randomly generated.

<?php
session_start();
if ($_POST["vercode"] != $_SESSION["vercode"] OR $_SESSION["vercode"]==”)  {
echo  ‘<strong>Incorrect verification code.</strong><br>’;
} else {
// add form data processing code here
echo  ‘<strong>Verification successful.</strong><br>’;
};
?>

Best Credit to : http://www.phpjabbers.com/captcha-image-verification-php19.html

Categories: php Tags: , , , ,
  1. No comments yet.
  1. No trackbacks yet.
You must be logged in to post a comment.