作者:hutuworm 来源:糊涂馋寺
目前,不少网站为了防止用户利用机器人自动注册、登录、灌水,都采用了
验证码技术。所谓验证码,就是将一串随机产生的数字或符号,生成一幅图片,
图片里加上一些干扰象素(防止ocr),由用户肉眼识别其中的验证码信息,输
入表单提交网站验证,验证成功后才能使用某项功能。
我们这里展示了如何编写php程序实现验证码功能:
代码一:
<?php
/*
* filename: authpage.php
* author: hutuworm
* date: 2003-04-28
* @copyleft hutuworm.org
*/
srand((double)microtime()*1000000);
//验证用户输入是否和验证码一致
if(ist($http_post_vars[‘authinput’]))
{
if(strcmp($http_post_vars[‘authnum’],$http_post_vars[‘authinput’])==0)
echo “验证成功!”;
el
echo “验证失败!”;
}
//生成新的四位整数验证码
while(($authnum=rand()%10000)<1000)snowing;
?>
<form action=authpage.php method=post>
<table>
请输入验证码:<input type=text name=authinput style=”width: 80px̶学生社团联合会1;><br>
<input type=submit name=”验证” value=”提交验证码”>
<input type=hidden name=authnum value=<? echo $authnum; ?>>
<img src=authimg.php?authnum=<? echo $authnum; ?>>
</table>
</form>
代码二:
<?php
/*
* filename: authimg.php
* author: hutuworm
* date: 2003-04-28
* @copyleft hutuworm.org
*/
//生成验证码图片
header(“content-type: image/png”);
srand((double)microtime()*1000000);
$im = imagecreate(58,28);
$black = imagecolorallocate($im, 0,0,0);
$white = imagecolorallocate($im, 255,255,255);
$gray = imagecolorallocate($im, 200,200,200);
imagefill($im,68,30,$gray);
//将四位整数验证码绘入图片
imagestring($im, 5, 10, 8, $http_get_vars[‘authnum’], $black);
for($i=0;$i<50;$i++) //加入干扰象素
{
imagetpixel($im, rand()%70 , rand()%30 , $black);
}
imagepng($im);
imagedestroy($im);
?>
本文程序在apache 2.0.45 + php 4.3.1环境下运行通过。
上文只是对验证码功能的一个简单实现,并没有考虑商用安全性问题。如果要增强安全性,将此功能投入商业应用,则可以通过以下几个步骤实现:
1. 启用ssion。
2. authnum在authimg.php中生成,并计算md5sum,存入ssion。
3. authpage.php将authinput计算md5sum后,与ssion中的authnum(md5sum)对比得出验证结果。
本站注:作者使用了简单的代码实现了很酷的功能。不过在添加干扰像素时的效果不是太好,大家可以看一下雨声论坛登录时的效验码(/d/file/titlepic/1.click.com.cn
/*
* filename: authimg.php
* author: hutuworm
* date: 2003-04-28
* @copyleft hutuworm.org
*/
//生成验证码图片
header(“content-type: image/png”);
srand((double)microtime()*1000000);
$im = imagecreate(62,20);
$black = imagecolorallocate($im, 0,0,0);
$white =暑假趣事600字作文 imagecolorallocate($im, 255,255,255);
$gray = imagecolorallocate($im, 2申奥成功00,200,200);
imagefill($im,68,30,$gray);
while(($authnum=rand()%100000)<10000);
//将四位整数验证码绘入图片
imagestring($im, 5, 10, 3, $authnum, $black);
for($i=0;$i<200;$i++) //加入干扰象素
{
$randcolor = imagecolorallocate($im,rand(0,255),rand(0,255),rand(0,255));
imagetpixel($im本拉登与中国, rand()%70 , rand()%30 , $randcolor);
}
imagepng($im);
imagedestroy($im);
?>
本文发布于:2023-04-06 08:22:18,感谢您对本站的认可!
本文链接:https://www.wtabcd.cn/fanwen/zuowen/67d95096cac1d288415134f0ae95e0ee.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文word下载地址:用PHP实现验证码功能.doc
本文 PDF 下载地址:用PHP实现验证码功能.pdf
留言与评论(共有 0 条评论) |