<?php
session_start();
?>
<form action=authpage.php method=post>
<table>
请输入验证码:
<input type=text name=authinput style="width: 80px">
<input type=submit name="验证" value="提交验证码">
<input type=text name=authnum value=<?=$_SESSION['seccode'];?>>
<img src="a.php">
</table>
</form>
?>
上面是验证用的。
下面是生成图片验证
<?php
// --------------------------------------------------------------------------
// File name : seccode.php
// Description : Sort Info System
// Requirement : PHP4.2 (http://www.php.net)
//
// --------------------------------------------------------------------------
session_start();
$_SESSION['seccode'] = "";
$width = 45;//图片长度
$height = 20;//图片高度
$len = 4;//验证码长度
$noise = true;//是否生成杂点
$noisenum = 60;//杂点数量
$border = true;//边框
$bordercolor = '#000000';
$bgcolor = "#d6e3ef";//背景色
$image = imagecreate($width,$height);
$back = getcolor($bgcolor);
imagefilledrectangle($image,0,0,$width,$height,$back);
$size = $width/$len;
if($size>$height) $size=$height;
$left = ($width-$len*($size+$size/10))/$size;
$code = random($len);
$codecolor = imagecolorallocate($image, 0, 0, 0);
imagestring($image, $size, 3, 3, $code, $codecolor);
if($noise == true) setnoise();
$_SESSION['seccode'] = $code;
if($border) imageRectangle($image, 0, 0, $width-1, $height-1, getcolor($bordercolor));
header("Content-type: image/png");
imagePng($image);
imagedestroy($image);
function getcolor($color)
{
global $image;
$color = eregi_replace ("^#","",$color);
$r = $color[0].$color[1];
$r = hexdec ($r);
$b = $color[2].$color[3];
$b = hexdec ($b);
$g = $color[4].$color[5];
$g = hexdec ($g);
$color = imagecolorallocate ($image, $r, $b, $g);
return $color;
}
function random($length) {
$str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz';
$hash = '';
for($i = 0; $i < $length; $i++) {
$hash .= $str[mt_rand(0,61)];
}
return $hash;
}
function setnoise()
{
global $image, $width, $height, $back, $noisenum;
for ($i=0; $i<$noisenum; $i++){
$randColor = imagecolorallocate($image, rand(0, 255), rand(0, 255), rand(0, 255));
imagesetpixel($image, rand(0, $width), rand(0, $height), $randColor);
}
}
?>
session_start();
?>
<form action=authpage.php method=post>
<table>
请输入验证码:
<input type=text name=authinput style="width: 80px">
<input type=submit name="验证" value="提交验证码">
<input type=text name=authnum value=<?=$_SESSION['seccode'];?>>
<img src="a.php">
</table>
</form>
?>
上面是验证用的。
下面是生成图片验证
<?php
// --------------------------------------------------------------------------
// File name : seccode.php
// Description : Sort Info System
// Requirement : PHP4.2 (http://www.php.net)
//
// --------------------------------------------------------------------------
session_start();
$_SESSION['seccode'] = "";
$width = 45;//图片长度
$height = 20;//图片高度
$len = 4;//验证码长度
$noise = true;//是否生成杂点
$noisenum = 60;//杂点数量
$border = true;//边框
$bordercolor = '#000000';
$bgcolor = "#d6e3ef";//背景色
$image = imagecreate($width,$height);
$back = getcolor($bgcolor);
imagefilledrectangle($image,0,0,$width,$height,$back);
$size = $width/$len;
if($size>$height) $size=$height;
$left = ($width-$len*($size+$size/10))/$size;
$code = random($len);
$codecolor = imagecolorallocate($image, 0, 0, 0);
imagestring($image, $size, 3, 3, $code, $codecolor);
if($noise == true) setnoise();
$_SESSION['seccode'] = $code;
if($border) imageRectangle($image, 0, 0, $width-1, $height-1, getcolor($bordercolor));
header("Content-type: image/png");
imagePng($image);
imagedestroy($image);
function getcolor($color)
{
global $image;
$color = eregi_replace ("^#","",$color);
$r = $color[0].$color[1];
$r = hexdec ($r);
$b = $color[2].$color[3];
$b = hexdec ($b);
$g = $color[4].$color[5];
$g = hexdec ($g);
$color = imagecolorallocate ($image, $r, $b, $g);
return $color;
}
function random($length) {
$str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz';
$hash = '';
for($i = 0; $i < $length; $i++) {
$hash .= $str[mt_rand(0,61)];
}
return $hash;
}
function setnoise()
{
global $image, $width, $height, $back, $noisenum;
for ($i=0; $i<$noisenum; $i++){
$randColor = imagecolorallocate($image, rand(0, 255), rand(0, 255), rand(0, 255));
imagesetpixel($image, rand(0, $width), rand(0, $height), $randColor);
}
}
?>
<input type=text name=authnum value=""><img src="seccode.php"/>后面显示图片,让用户自己输入
$_SESSION['seccode'] = $code; 这句是关键
后台判断
if($_POST['authnum'] != $_SESSION['seccode']){
// 验证码错误...
}