PHP/Captcha
Written by: WimpySE , 2009-06-05 18:07:31
<?php
session_start();
if($_SESSION['code'] == $_POST['code'] && !empty($_POST['name']))
{
$message = 'Thank you '.$_POST['name'];
unset($_SESSION['code']);
}
else
{
$message = 'Wrong or missing code or missing name, try again!';
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>
Test Page
</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
</head>
<body>
<h3>Test Page</h3>
<form id="testform" method="post" action="">
Your name:
<br/>
<input type="text" name="name" id="name" />
<br /><br />
<img src="captcha.php" alt="captcha" />
<br />
<input type="text" name="code" id="code" />
<br /><br />
<input type="submit" name="submit" id="submit" value="Send" />
</form>
<p>
<?php echo $message; ?>
</p>
</body>
</html>
<?php
session_start();
class CaptchaImage
{
private $font = 'URL';
function GetCode()
{
}
function __construct($width, $height)
{
}
}
?>
<?php
session_start();
class CaptchaImage
{
...
function __counstruct($width, $height)
{
$image = @imagecreate($width, $height);
$white = imagecolorallocate($image, 255, 255, 255);
$black = imagecolorallocate($image, 0, 0, 0);
$gray = imagecolorallocate($image, 51, 51, 51);
}
}
?>
<?php
session_start();
class CaptchaImage
{
...
function __counstruct($width, $height)
{
...
$gray = imagecolorallocate($image, 51, 51, 51);
for($i = 0; $i < 750; $i++)
{
imagesetpixel($image, mt_rand(0,$width), mt_rand(0,$height), $gray);
}
for($i = 0; $i < 200; $i++)
{
imageline($image, mt_rand(0,$width), mt_rand(0,$height), mt_rand(0,$width), mt_rand(0,$height), $gray);
}
}
}
?>
<?php
session_start();
class CaptchaImage
{
...
function GetCode()
{
$chars = '1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$code = '';
for ($i = 0; $i <= 5; $i++)
{
$code .= substr($chars, mt_rand(0, strlen($chars)-1), 1);
}
$_SESSION['code'] = $code;
return $code;
}
...
}
?>
<?php
session_start();
class CaptchaImage
{
...
function __construct($width, $height)
{
...
for($i = 0; $i < 200; $i++)
{
imageline($image, mt_rand(0,$width), mt_rand(0,$height), mt_rand(0,$width), mt_rand(0,$height), $black);
}
$code = $this->GetCode();
$fontsize = $height * 0.5;
$txtbox = imagettfbbox($fontsize, 0, $this->font, $code);
$x = ($width - $txtbox[4])/2;
$y = ($height - $txtbox[5])/2;
}
}
?>
<?php
session_start();
class CaptchaImage
{
...
function __construct($width, $height)
{
...
$x = ($width - $txtbox[4])/2;
$y = ($height - $txtbox[5])/2;
imagettftext($image, $fontsize, 0, $x, $y, $black, $this->font, $code);
for($i = 0; $i < 150; $i++)
{
imagesetpixel($image, mt_rand(0,$width), mt_rand(0,$height), $white);
}
for($i = 0; $i < 75; $i++)
{
imagesetpixel($image, mt_rand(0,$width), mt_rand(0,$height), $gray);
}
}
}
?>
<?php
session_start();
class CaptchaImage
{
...
function __construct($width, $height)
{
...
for($i = 0; $i < 75; $i++)
{
imagesetpixel($image, mt_rand(0,$width), mt_rand(0,$height), $gray);
}
header('Content-Type: image/gif');
imagegif($image);
imagedestroy($image);
}
}
$width = 150;
$height = 75;
new CaptchaImage($width, $height);
?>
<?php
session_start();
class CaptchaImage
{
private $font = 'URL';
function GetCode()
{
$chars = '1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$code = '';
for ($i = 0; $i <= 5; $i++)
{
$code .= substr($chars, mt_rand(0, strlen($chars)-1), 1);
}
$_SESSION['code'] = $code;
return $code;
}
function __construct($width, $height)
{
$image = @imagecreate($width, $height);
$white = imagecolorallocate($image, 255, 255, 255);
$black = imagecolorallocate($image, 0, 0, 0);
$gray = imagecolorallocate($image, 51, 51, 51);
for($i = 0; $i < 750; $i++)
{
imagesetpixel($image, mt_rand(0,$width), mt_rand(0,$height), $gray);
}
for($i = 0; $i < 200; $i++)
{
imageline($image, mt_rand(0,$width), mt_rand(0,$height), mt_rand(0,$width), mt_rand(0,$height), $gray);
}
$code = $this->GetCode();
$fontsize = $height * 0.50;
$txtbox = imagettfbbox($fontsize, 0, $this->font, $code);
$x = ($width - $txtbox[4])/2;
$y = ($height - $txtbox[5])/2;
imagettftext($image, $fontsize, 0, $x, $y, $black, $this->font, $code);
for($i = 0; $i < 150; $i++)
{
imagesetpixel($image, mt_rand(0,$width), mt_rand(0,$height), $white);
}
for($i = 0; $i < 75; $i++)
{
imagesetpixel($image, mt_rand(0,$width), mt_rand(0,$height), $gray);
}
header('Content-Type: image/gif');
imagegif($image);
imagedestroy($image);
}
}
$width = 150;
$height = 75;
$image = new CaptchaImage($width, $height);
?>
There are no comments on this article.
If you have any question or just want to leave a message, just fill out the form below!
Your e-mail will not be visible in your post, it is for validation reasons only
Finds programming addictive and just can't stop coding. Fools around with C#/PHP/MySQL/JavaScript/DHTML/AJAX/CSS/XHTML and, at last and least, Flash...
Besides coding he, eventually but certainly not frequently enough, takes some time away from the world of computers to spend with his loved one...
He's just awesome! <3