Welcome

首页 / 网页编程 / PHP / PHP 用session与gd库实现简单验证码生成与验证的类方法

验证码是为了防止机器灌水给网站带来污染以及增加服务器负担而出现的。目前大大小小的网站都有验证码。今天自己实现了一个简单的验证码类。说简单是因为没有加一些干扰的弧线等等,只是将文字旋转了一下。当然,因为字体的原因,要想一眼看出来并不容易。同时,为了避免字母的大小写与数字混淆,又去掉了那些看起来很像的字母数字。
类:
<?php/** *简单生成验证码类 */class Captcha {private $width;//验证码宽度private $height;//验证码高度private $countOfChars;//字符数//private $distrubLines;//干扰线条数private $chars;//随机生成的字符串public function __construct($width=100,$height=30,$countOfChars=4,$distrubLines=2) {//初始化参数$this->width=$width;$this->height=$height;$this->countOfChars=$countOfChars;session_start();}/** * 执行全部动作,生成验证码并直接输出 */public function execute(){$imageHandle=$this->createImage();$this->createChars();$this->drawChars($imageHandle);$this->outImage($imageHandle);}/** * 创建画布,并随机填充颜色 * @return 返回画布句柄 */public function createImage(){$imageHandle= imagecreate($this->width, $this->height);//随机背景颜色$randColor=imagecolorallocate($imageHandle, 50, mt_rand(0, 50), mt_rand(0, 50));imagefill($imageHandle, 0, 0, $randColor);return $imageHandle;}/** * 生成随机字符 */private function createChars(){//候选字符$str="ABCDEFGHJKLMNPQRSTUVWXZabcdefghijkmnpqtuvwx2346789";$chars="";for($i=0;$i<$this->countOfChars;$i++){$chars.=$str[mt_rand(0,strlen($str)-1)];}$this->chars=$chars;//保存在会话中$_SESSION["captcha"]=strtolower($chars);}/** * 将字符写入图像 * @param type $imageHandle 图像句柄 */private function drawChars($imageHandle){if($this->chars!=null){$font="/home/WWW/YuWeiLiShuFT.ttf";for($i=0;$i<strlen($this->chars);$i++){$color= imagecolorallocate($imageHandle,mt_rand(50, 200),mt_rand(100, 255),255);imagefttext($imageHandle,30, 30,$i*20+10,25,$color,$font,$this->chars[$i]);}}}/** * 输出图像 * @param type $imageHandle 图像句柄 */private function outImage($imageHandle){imagepng($imageHandle);imagedestroy($imageHandle);}/** * 判断用户输入的验证码是否正确 * @param type $usrInput 用户的输入 * @return boolean 验证码是否匹配 */public static function isRight($usrInput){if(isset($_SESSION["captcha"])){if(strtolower($usrInput)==$_SESSION["captcha"]){$_SESSION["captcha"]=null;return true;}else{$_SESSION["captcha"]=null;return false;}}}}
把验证设置成了静态方法,因为生成验证码后已经把验证码存到了session中,验证时直接调用静态方法,而不需要实例化这个类了。
上面的字体可以随意设置。
下面的代码讲返回一个图像,实例化Captcha类后动态生成的一个图像。(outCaptcha.php)
<?phprequire("Captcha.php");$code= new Captcha();header("Content-Type:image/png");$code->execute();
header(‘Content-Type:image/png");
这句话的作用是告诉浏览器输出的是png图像,而不是html代码。浏览器收到后就将下面的输出解析成图像。
然后写一个html静态页面(testCaptcha.html),创建表单
<!DOCTYPE html><html><head><title>验证码测试</title><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"></head><body><h1>请输入验证码:</h1><img src="outCaptcha.php"/><form method="POST" action="prove.php"><input type="text" name="input_captcha"/><button name="submit">确定</button></form></body></html>
仅仅是这样是不够的,看到表单提交的地址了么?那个就是用来验证验证码是否输入正确的代码:
session_start();$inputCaptcha= trim($_POST["input_captcha"]);require("Captcha.php");if(Captcha::isRight($inputCaptcha)){echo "验证码正确";}else{echo "验证码错误或已过期";}session_destroy();
这里还是要导入Captcha这个类,然后调用其静态方法来验证你的输入。最后销毁整个会话。
最后看看效果吧


太好了,成功了。那再故意输错试试,后退一下,然后刷新(如果不刷新浏览器将直接调用缓存中的验证码图片,这个时候我们的验证码还没有生成呢!所以无论怎么样都出错)。


当然,真正的验证码是可以单击换一张的,这利用了ajax技术。
以上这篇PHP 用session与gd库实现简单验证码生成与验证的类方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。