Thursday, 28 June 2012

Captcha in simple php


When we build websites with forms their emerge situation where we want to include captcha to prevent it from spammers. here iam including a captcha file which can be used in simple php:-
create a catptcha.php and include the following code in this:-
<?php

session_start();
$text = rand(10000,99999);
$_SESSION["vercode"] = $text;
$height = 25;
$width = 65;

$image_p = imagecreate($width, $height);
$black = imagecolorallocate($image_p, 0, 0, 0);
$white = imagecolorallocate($image_p, 255, 255, 255);
$font_size = 14;

imagestring($image_p, $font_size, 5, 5, $text, $white);
imagejpeg($image_p, null, 80);
?>
and now for calling this capcha in your website call like this:-
<img src="captcha.php" alt="captchaimage">

and now for getting value of this captcha to check get the value by this:-
$text=$_SESSION["vercode"] ;
unset($_SESSION["vercode"]);
and now get the captcha entered by user by
$captcha        = $_POST['captcha'];

now check the values and do validation.

No comments:

Post a Comment