Here is a small PHP tutorial for you ….Hope you have noticed Image verification used in many sites to avoid spam.This is called CAPTCHA.I will tell you how you can make an Image verification for your website in PHP.I will guarantee you,its very simple and can be developed in 5 min.What we are doing is, we are generating a random number, writing it into an image and displaying it.See my previous article to know how we can draw an image in php
There are three files
<?php
$my_img = imagecreate( 100, 50 );
$background = imagecolorallocate( $my_img, 0, 0, 255 );
$text_colour = imagecolorallocate( $my_img, 255, 255, 0 );
$line_colour = imagecolorallocate( $my_img, 128, 255, 0 );
session_start();
$_SESSION['img_number']=rand(1000, 10000);
imagestring( $my_img, 4, 30, 25,$_SESSION['img_number'] ,
$text_colour );
imagesetthickness ( $my_img, 5 );
header( "Content-type: image/png" );
imagepng( $my_img );
imagedestroy( $my_img );
?>
<form action="result.php" method="post">
<img alt="Random Number" src="image.php">
<input type="text" name="num"><br>
<input type="submit" name="submit" value="Check">
</form>
<?php
session_start();
if($_SESSION['img_number'] != $_POST['num']){
echo'The number you entered doesn\'t match the image.<br>
<a href="form.php">Try Again</a><br>';
}else{
echo'The numbers Match!<br>
<a href="form.php">Try Again</a><br>';
}
?>
Image.php
This is the file in which the image is generated.The function Rand(1000,10000) generates
a random number between 1000 and 10000.Along with writing it to the image we are storing it as a session variable to validate with the users input
Form.php
In this page the image is fetched from image.php and displayed.There is a textbox for the users to enter number shown in the image
Result.php
This is the page in which the users input is compared with the random number generated and displayed in the image
I hope not much details are explanation is required ..Please comment on if you have any doubts.
Related posts:
- Draw image using php PHp makes it very easy for you to draw an image for you on the fly in a website.This can be used to draw...



Discussion
No comments for “Make Captcha using php in 5 minutes”