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-demo-data.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);
// change the number to change number of chars
$random_text= substr($string,0,8);
We change the header of the page as we are sending an image ( not an html code ) $im = @ImageCreate (80, 20)
or die ("Cannot Initialize new GD image stream");
// Assign background color
$background_color = ImageColorAllocate ($im, 204, 204, 204);
// text color is given
$text_color = ImageColorAllocate ($im, 51, 51, 255);
// Random string from session added
ImageString($im,5,5,2,$_SESSION['my_captcha'],$text_color);
ImagePng ($im); // image displayed
imagedestroy($im); // Memory allocation for the image is removed.
<html>
<head>
<title>Demo of a form showing Captcha image </title>
<script type="text/javascript">
function reload()
{
img = document.getElementById("capt");
img.src="captcha-image.php?rand_number=" + Math.random();
}
</script>
</head>
<body >
<form type=post action=captcha-demo-data.php><input type=text name=t1>
<img src=captcha-image.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-demo-data.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>
$im = @ImageCreate (80, 20) // Width and height of the image.The text we are placing inside this image here
ImageString($im,5,5,2,$_SESSION['my_captcha'],$text_color);Inside this function we have Int 5 as font size, Int 5 as X position of text inside Image, Int 2 as Y position. By adjusting these values we can align text to center , increase size of font and better position them.
How to add Text String to Image
At the time of random text generation, we have one option to select number of chars we want to use. This can be changed here.$random_text= substr($string,0,8); // change the number to change number of chars
Based on the number of chars used, font size and image with the text string can be positioned.
cva | 01-03-2013 |
thanks a lot |
HZ_92 | 18-10-2014 |
Simple and easy captch code :D Thank you very much! |
Hamza Ayub | 05-11-2014 |
Good Work |
seerin thaj | 13-03-2015 |
It is easy to understand |
Moin | 05-11-2016 |
How to set the image in center and how to made a font size.? |
siteadmin | 19-02-2017 |
Check the line saying ImageString($im,5,5,2, $_SESSION['my_captcha'],$text_color); Here first 5 is the font size, then 5 is the x position. Based on the number of text you want to display and the font size you can adjust the x position to keep the text string at center. It can be created dynamically but that will make the script complex. |
adib | 26-03-2018 |
How to validate this captcha. When user insert wrong input and click submit, error messages will appear but still in the same page |
smo1234 | 04-09-2018 |
That requires JQuery, we will develop that in Part II of this script. |
smo1234 | 05-09-2018 |
3rd Demo is added, here JQuery is used so page is not reloaded and validation message is shown to the user. This demo is included in Downloaded Zip file. |
anupama | 30-10-2018 |
Captcha image is not showing |
smo1234 | 31-10-2018 |
GD support must be available |
01-09-2021 | |
I was trying to add the captcha in my websites contact us form in But imagecreatetruecolor is not defined what can i do |
04-09-2021 | |
Check GD support , |