Tuesday, 3 April 2012

how to make captcha in php?

Captcha Code:

<?php
session_start();

$num='';

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

$_SESSION["num1"]=$num;

$img=imagecreatefrompng('captcha.png');
$color=imagecolorallocate($img,255,255,255);
$color1=imagecolorallocate($img,0,0,0);
imagestring($img,9,10,3,$num,30);
$fontsize=20;
$fontcolor = imagecolorallocate($img, 0, 0, 0);
$x = 0;
$y = $fontsize;

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

?>

How to use in php?

<img id="cap" style="margin-left: 15px;" src="comman/captcha.php" alt="" />

How to refresh captcha?

Call the below function  onclick

<script >
function captcha()
{
document.getElementById('cap').src = document.getElementById('cap').src+ '?';
}
</script>

Save as captcha.png file

2 comments:

  1. Thank you, for giving us good knowledge about captcha in php.

    ReplyDelete
  2. Thanks, for giving capture. it is help full but when we reload a page it remain same as last key.

    ReplyDelete