我想做一个校验码的页面程序,下面是我的主程序test.php:
<script language="JavaScript">
function checkreg()
{
if (form1.admin_check.value=="" )
{
alert("校验码不能为空!");
form1.admin_check.focus();
return false;
}}
</script>
<FORM action="admin_check.php" name="form1" method=post onsubmit="return checkreg()">
<TD vAlign=center align=right width="40%">请输入校验码 </TD>
<TD vAlign=center><INPUT type=text size=5 name=admin_check value="<? print $info_self["admin_check"]; ?>"> <img src="check.php">
</TD>
<INPUT type=submit value="提 交" name=submit> 实现校验码的程序为check.php:
<?php
session_start();
session_register('code');
$width = "70";//图片宽
$height = "25";//图片高
$len = "4";//生成几位验证码
$bgcolor = "#ffffff";//背景色
$noise = true;//生成杂点
$noisenum = 200;//杂点数量
$border = false;//边框
$bordercolor = "#000000";
$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;
for ($i=0; $i<$len; $i++)
{
$randtext = rand(0, 9);
$code .= $randtext;
$textColor = imageColorAllocate($image, rand(0, 100), rand(0, 100), rand(0, 100));
$font = rand(1,4).".ttf";
$randsize = rand($size-$size/10, $size+$size/10);
$location = $left+($i*$size+$size/10);
imagettftext($image, $randsize, rand(-18,18), $location, rand($size-$size/10, $size+$size/10), $textColor, $font, $randtext);
}
if($noise == true) setnoise();
$_SESSION['code'] = $code;
$bordercolor = getcolor($bordercolor);
if($border==true) imageRectangle($image, 0, 0, $width-1, $height-1, $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 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);
}
}
?> 校验码的判断程序为admin_check.php:
<?php
$img_entercode = $_POST["admin_check"];
session_start();
$img_randcode = $_SESSION["code"];
echo $_SESSION['code'];if ($img_entercode == $img_randcode)
{
echo "校验码输入正确!";
}
else echo "校验码不正确!";
?>但是我始终得不到正确的校验效果,请哪位高手帮忙看看,小弟不胜感激啊!
<script language="JavaScript">
function checkreg()
{
if (form1.admin_check.value=="" )
{
alert("校验码不能为空!");
form1.admin_check.focus();
return false;
}}
</script>
<FORM action="admin_check.php" name="form1" method=post onsubmit="return checkreg()">
<TD vAlign=center align=right width="40%">请输入校验码 </TD>
<TD vAlign=center><INPUT type=text size=5 name=admin_check value="<? print $info_self["admin_check"]; ?>"> <img src="check.php">
</TD>
<INPUT type=submit value="提 交" name=submit> 实现校验码的程序为check.php:
<?php
session_start();
session_register('code');
$width = "70";//图片宽
$height = "25";//图片高
$len = "4";//生成几位验证码
$bgcolor = "#ffffff";//背景色
$noise = true;//生成杂点
$noisenum = 200;//杂点数量
$border = false;//边框
$bordercolor = "#000000";
$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;
for ($i=0; $i<$len; $i++)
{
$randtext = rand(0, 9);
$code .= $randtext;
$textColor = imageColorAllocate($image, rand(0, 100), rand(0, 100), rand(0, 100));
$font = rand(1,4).".ttf";
$randsize = rand($size-$size/10, $size+$size/10);
$location = $left+($i*$size+$size/10);
imagettftext($image, $randsize, rand(-18,18), $location, rand($size-$size/10, $size+$size/10), $textColor, $font, $randtext);
}
if($noise == true) setnoise();
$_SESSION['code'] = $code;
$bordercolor = getcolor($bordercolor);
if($border==true) imageRectangle($image, 0, 0, $width-1, $height-1, $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 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);
}
}
?> 校验码的判断程序为admin_check.php:
<?php
$img_entercode = $_POST["admin_check"];
session_start();
$img_randcode = $_SESSION["code"];
echo $_SESSION['code'];if ($img_entercode == $img_randcode)
{
echo "校验码输入正确!";
}
else echo "校验码不正确!";
?>但是我始终得不到正确的校验效果,请哪位高手帮忙看看,小弟不胜感激啊!
这里显示的对否?
1) 输出$_session['code']
2) 输出$_post['code']
3) 输出($_session['code'] == $_post['code'])
4) 检查session的ini选项
2、在 $_SESSION['code'] = $code; 前加上 unset( $_SESSION['code'] );再试试?btw: session_register 系列函数和 $_SESSION 不要混用
for ($i=0; $i<$len; $i++)在你的FOR前面加$code = "";同意楼上的!
因为你每刷新一次都会在$_SEESION['code'] 后面加随机数
而不是由空串$code后面加
// Use of session_register() is deprecated
$barney = "A big purple dinosaur.";
session_register("barney");// Use of $_SESSION is preferred, as of PHP 4.1.0
$_SESSION["zim"] = "An invader from another planet.";// The old way was to use $HTTP_SESSION_VARS
$HTTP_SESSION_VARS["spongebob"] = "He's got square pants.";
?> 手册都建议你不要用session_register()了,你还有什么理由去用它呢
有兴趣帮助更多的初学者,来我们的QQ群:33918040