« File uploading with PHPPut watermark on images using PHP »
<?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);
?>
<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>
<?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>';
};
?>
Write tutorial on a topic you are good in and become a trusted PHP jabber! Share your knowledge with thousands of webmasters and we will reward you for your generosity by giving you bonus points which you can use as a voucher to buy any of our commercial products. Read more about our reward program.
218 Replies to "Captcha image verification"
<?php
session_start ();
$t1= rand(A,Z);
$t2= rand (a,z);
$t3= rand(100,500);
$tx= "$t1"."$t2"."$t3" ;
$_SESSION['cap']= $tx;
header ("content-type: image/jpeg");
$w =80;
$h=30;
$image=imagecreate ($w,$h);
$b1=imagecolorallocate ($image, 00,00,00);
$ww=imagecolorallocate ($image, 255,255,255);
$f=15;
imagestring ($image,$f,7,7,$tx,$ww);
imagejpeg ($image, null, 50);
?>
// IMPORTANT!
// Send ALL headers before session_start() is invoked
header ("content-type: image/jpeg");
session_start ();
// Function returns random symbol string
// Accepts symbol quantity of the returned string
function randomCaptcha($symbol_qty){
// Use range() instead of random for strings
$symbol_list = array_merge(range('a', 'z'), range('A', 'Z'), range('0', '25'));
shuffle($symbol_list);
foreach (array_rand($symbol_list, $symbol_qty) as $v) {
$random_captcha .= $symbol_list[$v];
}
return $random_captcha;
}
// Get a random string with 5 characters in it
$tx = randomCaptcha(5);
// Thats all, here goes your code ;)
$_SESSION['cap']= $tx;
$w =80;
$h=30;
$image=imagecreate ($w,$h);
$b1=imagecolorallocate ($image, 00, 00, 00);
$ww=imagecolorallocate ($image, 255, 255, 255);
$f=15;
imagestring ($image,$f,7,7,$tx,$ww);
imagejpeg ($image, null, 50);