You may see the image captcha in the forms when you signup in google , wordpress or other networks. Do you ever think how this images are generated and how it is useful . First security , it give high security to protect forms and website from hackers. Second how it works, it will be cool and exiting ! please view the code below and understand. THINGS NEEDED 1. comic.tff FILES TO CREATE 1. captca.php 2. form.html
captcha.php
<?php
session_start();
define('font' , 'comic.ttf');
function generate($characters) {
$possible = '23456789ABCDEFGHIJKL!';
$code = '';
$i = 0;
while ($i < $characters) {
$code .= substr($possible, mt_rand(0, strlen($possible)-1), 1);
$i++;
}
return $code;}
function Captcha($width='120',$height='40',$characters='5') {
$code = generate($characters);
$font_size = $height * 0.75;
$image = @imagecreate($width, $height) or die('Cannot initialize new GD image stream');
$background_color = imagecolorallocate($image, 225, 255, 255);
$text_color = imagecolorallocate($image, 20, 40, 100);
$noise_color = imagecolorallocate($image, 160, 149, 249);
for( $i=0; $i<($width*$height)/3; $i++ ) {
imagefilledellipse($image, mt_rand(0,$width), mt_rand(0,$height), 1, 1, $noise_color);}
for( $i=0; $i<($width*$height)/150; $i++ ) {
imageline($image, mt_rand(0,$width), mt_rand(0,$height), mt_rand(0,$width), mt_rand(0,$height), $noise_color);
}
$textbox = imagettfbbox($font_size, 0, font, $code);
$x = ($width - $textbox[4])/2;
$y = ($height - $textbox[5])/2;
imagettftext($image, $font_size, 0, $x, $y, $text_color, font , $code);
header('Content-Type: image/jpeg');
imagejpeg($image);
imagedestroy($image);
$_SESSION['captcha'] = $code; }
Captcha();
?>
session_start();
define('font' , 'comic.ttf');
function generate($characters) {
$possible = '23456789ABCDEFGHIJKL!';
$code = '';
$i = 0;
while ($i < $characters) {
$code .= substr($possible, mt_rand(0, strlen($possible)-1), 1);
$i++;
}
return $code;}
function Captcha($width='120',$height='40',$characters='5') {
$code = generate($characters);
$font_size = $height * 0.75;
$image = @imagecreate($width, $height) or die('Cannot initialize new GD image stream');
$background_color = imagecolorallocate($image, 225, 255, 255);
$text_color = imagecolorallocate($image, 20, 40, 100);
$noise_color = imagecolorallocate($image, 160, 149, 249);
for( $i=0; $i<($width*$height)/3; $i++ ) {
imagefilledellipse($image, mt_rand(0,$width), mt_rand(0,$height), 1, 1, $noise_color);}
for( $i=0; $i<($width*$height)/150; $i++ ) {
imageline($image, mt_rand(0,$width), mt_rand(0,$height), mt_rand(0,$width), mt_rand(0,$height), $noise_color);
}
$textbox = imagettfbbox($font_size, 0, font, $code);
$x = ($width - $textbox[4])/2;
$y = ($height - $textbox[5])/2;
imagettftext($image, $font_size, 0, $x, $y, $text_color, font , $code);
header('Content-Type: image/jpeg');
imagejpeg($image);
imagedestroy($image);
$_SESSION['captcha'] = $code; }
Captcha();
?>
form.html
<
form
method
=
"post"
action
=
"check.php"
>
NAME : <
input
name
=
"name"
>
captch : <
img
src
=
"captcha.php"
> <
input
name
=
"cap_code"
>
<
br
><
input
type
=
"submit"
value
=
"submit"
>
</
form
>
The captcha image will save the code in the user browser in the name captcha. To retrive the data use this script.THINGS TO REMEMBER
check.php
<?php
session_start();
if
(isset(
$_SESSION
[
'captcha'
])) {
if
(
$_POST
[
'cap_code'
]==
$_SESSION
[
'captcha'
])
{
echo
'<font color="green">correct captcha</font>'
;
}
else
echo
'<font color="red">worng captcha</font>'
;
}
?>
No comments:
Post a Comment