SQL PHP HTML ASP JavaScript articles and free scripts to download
PHP GD

Captcha generation script in PHP

This is a simple captcha script which we will use to develop using PHP and GD library functions. To keep the script simple we have not used many powerful tools available in GD library. After understanding this basic tutorial we can go for advance captcha generation script. There are three files used,
  1. captcha-demo.php , the main PHP script which generates the captcha image.
  2. captcha-demo1.php , the form which display the captcha image generated by above PHP script
  3. captcha-demo2.php, output of is displayed once the user submit the form.
We will discuss each file but before that you can check the demo here.

Demo of Captcha generation script

Download source code of this demo at end of the page

Inside PHP script first we will destroy any previous session and then crate a new session which we will use to store random string. This randomly generated string we will add to our image and ask the visitor to read and enter in the text box provided.
session_start();
if(isset($_SESSION['my_captcha']))
{
unset($_SESSION['my_captcha']); // destroy the session if already there
}
We store the randomly generated string in a session variable and we use the same string to generate the captcha image. The session variable is available in our captcha-demo2.php page where we match both data in session variable and user entered string.
$string1="abcdefghijklmnopqrstuvwxyz";
$string2="1234567890";
$string=$string1.$string2;
$string= str_shuffle($string);
$random_text= substr($string,0,8); // change the number to change number of chars
We change the header of the page as we are sending an image ( not an html code )
In the last part of the script we will create the image using GD library support.
$im = @ImageCreate (80, 20)
or die ("Cannot Initialize new GD image stream");
$background_color = ImageColorAllocate ($im, 204, 204, 204); // Assign background color
$text_color = ImageColorAllocate ($im, 51, 51, 255);      // text color is given 
ImageString($im,5,5,2,$_SESSION['my_captcha'],$text_color); // Random string  from session added 

ImagePng ($im); // image displayed
imagedestroy($im); // Memory allocation for the image is removed. 
We have a simple web form inside which we will include the PHP file captcha-demo.php to display the image.
<html>
<head>
<title>Demo of a  form showing  Captcha image with random string to user read and enter</title>
<script type="text/javascript">
function reload()
{
img = document.getElementById("capt");
img.src="captcha-demo.php?rand_number=" + Math.random();
}
</script>

</head>
<body >

<form type=post action=captcha-demo2.php><input type=text name=t1>
<img src=captcha-demo.php id="capt">
<input type=button onClick=reload(); value='Reload image'><input type=submit value='submit'>
</form>
</body>
</html>
We have added one button to reload the image by user if required. Now once user submit the form the details will be displayed at captcha-demo2.php file. This file collects all the form data and using one if condition check if captcha string ( present in the session variable ) is same as user entered string or not.
<?Php
session_start();
?>
<!doctype html public "-//w3c//dtd html 3.2//en">

<html>
<head>
<title>demo of displaying form value showing captcha image</title>
</head>

<body >
<?Php
echo "Text as entered by user  : $_GET[t1] <br>";
echo "Captcha shown : $_SESSION[my_captcha] <br>";
if($_GET['t1'] == $_SESSION['my_captcha']){
echo "Captcha validation passed ";
}else {
echo "Captcha validation failed ";
}
unset($_SESSION['my_captcha']);

?>

</body>
</html>

Download source code of captcah generation script

Number of User Comments : 1


Google+
cva01-03-2013
thanks a lot
Post Comment This is for short comments only. Use the forum for more discussions.
Name
Email( not to be displayed)Privacy Policy
1+2=This is to prevent automatic submission by spammers. Please enter the result of the sum as asked



HTML . MySQL. PHP. JavaScript. ASP. Photoshop. Articles. FORUM Contact us

©2000-2014 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer