php 2007-7-14 10:29
用PHP实现验证码功能
用PHP实现验证码功能
作者:hutuworm
目前,不少网站为了防止用户利用机器人自动注册、登录、灌水,都采用了
验证码技术。所谓验证码,就是将一串随机产生的数字或符号,生成一幅图片,
图片里加上一些干扰象素(防止OCR),由用户肉眼识别其中的验证码信息,输
入表单提交网站验证,验证成功后才能使用某项功能。
我们这里展示了如何编写PHP程序实现验证码功能:
代码一:
[color=#000000][color=#0000cc]<[/color][color=#0000cc]?[/color]
[color=#ff9900]/*
* Filename: authpage.php
* Author: hutuworm
* Date: 2003-04-28
* @Copyleft hutuworm.org
*/[/color]
[color=#ff0000]srand[/color][color=#0000cc]([/color][color=#0000cc]([/color]double[color=#0000cc])[/color][color=#ff0000]microtime[/color][color=#0000cc]([/color][color=#0000cc])[/color][color=#0000cc]*[/color]1000000[color=#0000cc])[/color][color=#0000cc];[/color]
[color=#ff9900]//验证用户输入是否和验证码一致
[/color]
[color=#0000ff]if[/color][color=#0000cc]([/color][color=#ff0000]isset[/color][color=#0000cc]([/color][color=#0000ff]$[/color][color=#008080]HTTP_POST_VARS[/color][color=#0000cc][[/color][color=#ff00ff]'authinput'[/color][color=#0000cc]][/color][color=#0000cc])[/color][color=#0000cc])[/color]
[color=#0000cc]{[/color]
[color=#0000ff]if[/color][color=#0000cc]([/color][color=#ff0000]strcmp[/color][color=#0000cc]([/color][color=#0000ff]$[/color][color=#008080]HTTP_POST_VARS[/color][color=#0000cc][[/color][color=#ff00ff]'authnum'[/color][color=#0000cc]][/color][color=#0000cc],[/color][color=#0000ff]$[/color][color=#008080]HTTP_POST_VARS[/color][color=#0000cc][[/color][color=#ff00ff]'authinput'[/color][color=#0000cc]][/color][color=#0000cc])[/color][color=#0000cc]=[/color][color=#0000cc]=[/color]0[color=#0000cc])[/color]
[color=#ff0000]echo[/color] [color=#ff00ff]"验证成功!"[/color][color=#0000cc];[/color]
[color=#0000ff]else[/color]
[color=#ff0000]echo[/color] [color=#ff00ff]"验证失败!"[/color][color=#0000cc];[/color]
[color=#0000cc]}[/color]
[color=#ff9900]//生成新的四位整数验证码
[/color]
[color=#0000ff]while[/color][color=#0000cc]([/color][color=#0000cc]([/color][color=#0000ff]$[/color][color=#008080]authnum[/color][color=#0000cc]=[/color][color=#ff0000]rand[/color][color=#0000cc]([/color][color=#0000cc])[/color][color=#0000cc]%[/color]10000[color=#0000cc])[/color][color=#0000cc]<[/color]1000[color=#0000cc])[/color][color=#0000cc];[/color]
[color=#0000cc]?[/color][color=#0000cc]>[/color]
[color=#0000cc]<[/color][color=#0000ff]form[/color] [color=#ff0000]action[/color][color=#0000cc]=[/color]authpage[color=#0000cc].[/color]php [color=#ff0000]method[/color][color=#0000cc]=[/color]post[color=#0000cc]>[/color]
[color=#0000cc]<[/color][color=#0000ff]table[/color][color=#0000cc]>[/color]
请输入验证码:[color=#0000cc]<[/color][color=#0000ff]input[/color] [color=#ff0000]type[/color][color=#0000cc]=[/color][color=#ff0000]text[/color] [color=#ff0000]name[/color][color=#0000cc]=[/color]authinput [color=#0000ff]style[/color][color=#0000cc]=[/color][color=#ff00ff]"width: 80px"[/color][color=#0000cc]>[/color][color=#0000cc]<[/color][color=#0000ff]br[/color][color=#0000cc]>[/color]
[color=#0000cc]<[/color][color=#0000ff]input[/color] [color=#ff0000]type[/color][color=#0000cc]=[/color]submit [color=#ff0000]name[/color][color=#0000cc]=[/color][color=#ff00ff]"验证"[/color] [color=#ff0000]value[/color][color=#0000cc]=[/color][color=#ff00ff]"提交验证码"[/color][color=#0000cc]>[/color]
[color=#0000cc]<[/color][color=#0000ff]input[/color] [color=#ff0000]type[/color][color=#0000cc]=[/color]hidden [color=#ff0000]name[/color][color=#0000cc]=[/color]authnum [color=#ff0000]value[/color][color=#0000cc]=[/color][color=#0000cc]<[/color][color=#0000cc]?[/color] [color=#ff0000]echo[/color] [color=#0000ff]$[/color][color=#008080]authnum[/color][color=#0000cc];[/color] [color=#0000cc]?[/color][color=#0000cc]>[/color][color=#0000cc]>[/color]
[color=#0000cc]<[/color][color=#0000ff]img[/color] [color=#ff0000]src[/color][color=#0000cc]=[/color]authimg[color=#0000cc].[/color]php[color=#0000cc]?[/color]authnum[color=#0000cc]=[/color][color=#0000cc]<[/color][color=#0000cc]?[/color] [color=#ff0000]echo[/color] [color=#0000ff]$[/color][color=#008080]authnum[/color][color=#0000cc];[/color] [color=#0000cc]?[/color][color=#0000cc]>[/color][color=#0000cc]>[/color]
[color=#0000cc]<[/color][color=#0000cc]/[/color][color=#0000ff]table[/color][color=#0000cc]>[/color]
[color=#0000cc]<[/color][color=#0000cc]/[/color][color=#0000ff]form[/color][color=#0000cc]>[/color]
[/color]
代码二:
[color=#000000][color=#0000cc]<[/color][color=#0000cc]?[/color]
[color=#ff9900]/*
* Filename: authimg.php
* Author: hutuworm
* Date: 2003-04-28
* @Copyleft hutuworm.org
*/[/color]
[color=#ff9900]//生成验证码图片
[/color]
[color=#ff0000]Header[/color][color=#0000cc]([/color][color=#ff00ff]"Content-type: image/PNG"[/color][color=#0000cc])[/color][color=#0000cc];[/color]
[color=#ff0000]srand[/color][color=#0000cc]([/color][color=#0000cc]([/color]double[color=#0000cc])[/color][color=#ff0000]microtime[/color][color=#0000cc]([/color][color=#0000cc])[/color][color=#0000cc]*[/color]1000000[color=#0000cc])[/color][color=#0000cc];[/color]
[color=#0000ff]$[/color][color=#008080]im[/color] [color=#0000cc]=[/color] [color=#ff0000]imagecreate[/color][color=#0000cc]([/color]58[color=#0000cc],[/color]28[color=#0000cc])[/color][color=#0000cc];[/color]
[color=#0000ff]$[/color][color=#008080]black[/color] [color=#0000cc]=[/color] [color=#ff0000]ImageColorAllocate[/color][color=#0000cc]([/color][color=#0000ff]$[/color][color=#008080]im[/color][color=#0000cc],[/color] 0[color=#0000cc],[/color]0[color=#0000cc],[/color]0[color=#0000cc])[/color][color=#0000cc];[/color]
[color=#0000ff]$[/color][color=#008080]white[/color] [color=#0000cc]=[/color] [color=#ff0000]ImageColorAllocate[/color][color=#0000cc]([/color][color=#0000ff]$[/color][color=#008080]im[/color][color=#0000cc],[/color] 255[color=#0000cc],[/color]255[color=#0000cc],[/color]255[color=#0000cc])[/color][color=#0000cc];[/color]
[color=#0000ff]$[/color][color=#008080]gray[/color] [color=#0000cc]=[/color] [color=#ff0000]ImageColorAllocate[/color][color=#0000cc]([/color][color=#0000ff]$[/color][color=#008080]im[/color][color=#0000cc],[/color] 200[color=#0000cc],[/color]200[color=#0000cc],[/color]200[color=#0000cc])[/color][color=#0000cc];[/color]
[color=#ff0000]imagefill[/color][color=#0000cc]([/color][color=#0000ff]$[/color][color=#008080]im[/color][color=#0000cc],[/color]68[color=#0000cc],[/color]30[color=#0000cc],[/color][color=#0000ff]$[/color][color=#008080]gray[/color][color=#0000cc])[/color][color=#0000cc];[/color]
[color=#ff9900]//将四位整数验证码绘入图片
[/color]
[color=#ff0000]imagestring[/color][color=#0000cc]([/color][color=#0000ff]$[/color][color=#008080]im[/color][color=#0000cc],[/color] 5[color=#0000cc],[/color] 10[color=#0000cc],[/color] 8[color=#0000cc],[/color] [color=#0000ff]$[/color][color=#008080]HTTP_GET_VARS[/color][color=#0000cc][[/color][color=#ff00ff]'authnum'[/color][color=#0000cc]][/color][color=#0000cc],[/color] [color=#0000ff]$[/color][color=#008080]black[/color][color=#0000cc])[/color][color=#0000cc];[/color]
[color=#0000ff]for[/color][color=#0000cc]([/color][color=#0000ff]$[/color][color=#008080]i[/color][color=#0000cc]=[/color]0[color=#0000cc];[/color][color=#0000ff]$[/color][color=#008080]i[/color][color=#0000cc]<[/color]50[color=#0000cc];[/color][color=#0000ff]$[/color][color=#008080]i[/color][color=#0000cc]+[/color][color=#0000cc]+[/color][color=#0000cc])[/color] [color=#ff9900]//加入干扰象素
[/color]
[color=#0000cc]{[/color]
[color=#ff0000]imagesetpixel[/color][color=#0000cc]([/color][color=#0000ff]$[/color][color=#008080]im[/color][color=#0000cc],[/color] [color=#ff0000]rand[/color][color=#0000cc]([/color][color=#0000cc])[/color][color=#0000cc]%[/color]70 [color=#0000cc],[/color] [color=#ff0000]rand[/color][color=#0000cc]([/color][color=#0000cc])[/color][color=#0000cc]%[/color]30 [color=#0000cc],[/color] [color=#0000ff]$[/color][color=#008080]black[/color][color=#0000cc])[/color][color=#0000cc];[/color]
[color=#0000cc]}[/color]
[color=#ff0000]ImagePNG[/color][color=#0000cc]([/color][color=#0000ff]$[/color][color=#008080]im[/color][color=#0000cc])[/color][color=#0000cc];[/color]
[color=#ff0000]ImageDestroy[/color][color=#0000cc]([/color][color=#0000ff]$[/color][color=#008080]im[/color][color=#0000cc])[/color][color=#0000cc];[/color]
[color=#0000cc]?[/color][color=#0000cc]>[/color]
[/color]
本文程序在Apache 2.0.45 + PHP 4.3.1环境下运行通过。