用php实现验证码功能
				
				
					
					
用PHP实现验证码功能
--------------------------------------------------------------------------------
 
作者:hutuworm 来源:糊涂馋寺 
  目前,不少网站为了防止用户利用机器人自动注册、登录、灌水,都采用了 
验证码技术。所谓验证码,就是将一串随机产生的数字或符号,生成一幅图片, 
图片里加上一些干扰象素(防止OCR),由用户肉眼识别其中的验证码信息,输 
入表单提交网站验证,验证成功后才能使用某项功能。 
  我们这里展示了如何编写PHP程序实现验证码功能: 
  代码一:  
    <?php 
   /* 
    *   Filename:    authpage.php 
    *   Author:   hutuworm 
    *   Date:   2003-04-28 
    *   @Copyleft    hutuworm.org 
    */ 
    srand((double)microtime()*1000000); 
   //验证用户输入是否和验证码一致 
        if(isset($HTTP_POST_VARS['authinput']))  
        { 
                if(strcmp($HTTP_POST_VARS['authnum'],$HTTP_POST_VARS['authinput'])==0) 
                        echo "验证成功!"; 
                else 
                        echo "验证失败!"; 
        } 
    
   //生成新的四位整数验证码 
        while(($authnum=rand()%10000)<1000);  
    ?> 
        <form action=authpage.php method=post> 
        <table> 
                请输入验证码:<input type=text name=authinput style="width: 80px"><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++)   //加入干扰象素 
        { 
                imagesetpixel($im, rand()%70 , rand()%30 , $black); 
        } 
        ImagePNG($im); 
        ImageDestroy($im); 
  ?> 
  本文程序在Apache 2.0.45 + PHP 4.3.1环境下运行通过。 
上文只是对验证码功能的一个简单实现,并没有考虑商用安全性问题。如果要增强安全性,将此功能投入商业应用,则可以通过以下几个步骤实现: 
1. 启用Session。 
2. authnum在authimg.php中生成,并计算md5sum,存入session。 
3. authpage.php将authinput计算md5sum后,与session中的authnum(md5sum)对比得出验证结果。
本站注:作者使用了简单的代码实现了很酷的功能。不过在添加干扰像素时的效果不是太好,大家可以看一下雨声论坛登录时的效验码(http://ror.cn/perl/ut/user_login.cgi),偶把第二段代码稍改了一下,生成了与其类似的效果。
修改后的代码如下:
<?php 
/* 
 *   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 = ImageColorAllocate($im, 255,255,255); 
$gray = ImageColorAllocate($im, 200,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));
    imagesetpixel($im, rand()%70 , rand()%30 , $randcolor); 
} 
ImagePNG($im); 
ImageDestroy($im); 
?>
显示结果如下图:
有兴趣的朋友可以自己试一下。
 
(责任编辑:超越PHP) 
[推荐给朋友] [显示打印版本]  更新日期:2003-05-17 浏览次数:4803 
  
用户评论 点击这里添加新评论 
titan3<titan3 at tom dot com>
26-Aug-2003 02:17 
 
今天升级php到php4.3.3,authimg.php的代码就不好用了,
生成的图片是黑漆漆一片,改了下代码,正常了:
//生成验证码图片
Header("Content-type: image/PNG"); 
srand((double)microtime()*1000000); 
$im = imagecreate(62,20);
$black = ImageColorAllocate($im, 0,0,0); 
$white =ImageColorAllocate($im, 255,255,255); 
$gray = ImageColorAllocate($im,200,200,200); 
imagefill($im,0,0,$gray);    //改了这里的参数
 
 
waff<chinawaff at 163 dot com>
04-Oct-2003 03:11 
 
图片怎么都不出来!
请回复[email protected]
2003.104
 
 
MIRCO<ata at ioou dot com>
19-Dec-2003 09:55 
 
可能你的 php 没有 gd 支持。如果 有gd 的话是一定可以的。我已经改了代码,可靠性更好。
<?php
############################
#  MIRCO(PIEA)  
#  http://www.ioou.com
#  e-mail:[email protected]
############################
Header("Content-type:image/PNG");
srand((double)microtime()*1000000);
$im =imagecreate(72,15);
$black = ImageColorAllocate($im,0,0,0);
$white = ImageColorAllocate($im, 255,255,255);
$gray =ImageColorAllocate($im, 200,200,200);
imagefill($im,86,30,$gray); //imagefill($im,0,0,$gray);  
for($i=0;$i<600;$i++)
{
  $randcolor =ImageColorallocate($im,rand(10,255),rand(10,255),rand(10,255));
  imagesetpixel($im, rand()%90 , rand()%30 ,$randcolor);
}
#---------------------------
#  WRITE 10 BLACKLINE
#---------------------------
for($i=0;$i<6;$i++)
{
imageline($im,rand(0,75),rand(0,75),rand(0,75),rand(0,75),$black);
}
#---------------------------
#  WRITE 7 mumber
#---------------------------
//while(($authnum=rand()%100000)<10000);
$array="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
for($i=0;$i<6;$i++)
{
//substr 62
$authnum .=substr($array,rand(0,61),1);
}
imagestring($im, 6, 10, 0,$authnum,$black);
ImagePNG($im);
ImageDestroy($im);
?>
 
 
  
    相关文章 
用PHP函数解决SQL injection 
用PHP工具包expat解析XML 
使用phplib7.2中的Template处理BLOCK多重嵌套 
用PHP将数据导入到Foxmail 
使用PHP4中的 IntegratedTemplate类实现HTML和PHP代码分离 
使用PHP4中的 IntegratedTemplate类实现BLOCK功能 
用PHP4 和 PostgreSQL 构建一个电子商务应用 
用PHP制作静态网站的模板框架 
用php实现文件上传二法 
用PHP发送MIME邮件(六)