Home

Friday, December 30, 2011

PHP Captcha

Step-1 Captcha.php

<?php
session_start();
$num='';
$string = "abcdefghijklmnopqrstuvwxyz0123456789";

for($i=0;$i<7;$i++){
    $num1=rand(0,35);
    $num .= $string{$num1};
}

$_SESSION["num1"]=$num;
$img=imagecreate(90,27);
$color=imagecolorallocate($img,255,255,255);
$color1=imagecolorallocate($img,0,0,0);
imagestring($img,5,10,10,$num,30);
$fontsize=20;
$fontcolor = imagecolorallocate($img, 0, 0, 0);
$x = 0;
$y = $fontsize;

header('Content-type : image/png');
imagepng($img);
imagedestroy($img);
?>

Step-2 Register.php

<img src="captcha.php" id="captcha"  />
<a href="#" onClick="javascript:reloadCaptcha();" />>New Code</a>

Step-3 Add Javascript for New code (In Register Page)

<script type="text/javascript">
function reloadCaptcha()
{
   document.getElementById('captcha').src = document.getElementById('captcha').src+ '?';

}
</script>



2 comments:

  1. where is captcha image.

    ReplyDelete
  2. Hello Sir,
    If you want image display then

    $img=imagecreate(90,27)
    Replace with
    $img=imagecreatefromjpeg("03.jpg");

    ReplyDelete

rathoddhirendra.blogspot.com-Google pagerank and Worth