您的位置:首页 > 编程语言 > PHP开发

php判断用户输入验证码是否正确

2012-07-21 19:58 661 查看
yzm.php

<?php

//生成验证码图片

session_start();

Header("Content-type: image/gif");

srand((double)microtime()*1000000);

$im = imagecreate(45,18);//设置图片的宽与高

$black = ImageColorAllocate($im, 0,0,0);//设置背景颜色

$white = ImageColorAllocate($im, 255,255,255);//设置文字颜色

$gray = ImageColorAllocate($im, 200,200,200);//干扰颜色

imagefill($im,45,18,$gray);

while(($chk_num=rand()%10000)<1000);

$_SESSION["chk_num"] = $chk_num;

//将四位整数验证码绘入图片

imagestring($im, 5, 5, 1,$chk_num , $white);//5, 5, 1分别表示为字体大小,左边距,上边距

for($i=0;$i<100;$i++) //加入干扰象素

{

imagesetpixel($im, rand()%70 , rand()%30 , $gray);

}

ImagePNG($im);

ImageDestroy($im);

?>

yanzhengma.php

<html>

<head>

<title>验证码判断</title>

</head>

<body>

<form method="post" action="yanzhengma.php">

<input type="text" name="yzm" >

<img src="yzm.php" ><br>

<input type="submit" name="submit" value="验证">

<?php

error_reporting(0);

session_start();

if (isset($_POST['submit']))

{

echo "<br>".$_SESSION[yzm];

echo "<br>".$_POST['yzm'];

if ($_POST['yzm'] == $_SESSION[yzm])

{

echo "<br>"."OK!";

}

else

{

echo "<br>"."NO!";

}

}

?>

</form>

</body>

</html>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  颜色 white 200
相关文章推荐