1.验证码封装
<?php /** * 验证码封装 * @param int $type 类型1数字 2字母 3数字或字母 * @param int $leng 长度 * @param int $pixel 干扰点 * @param int $line 干扰线段 * @param int $arc 干扰圆弧 * @param int $snow 干扰雪花 * @param int $width 宽 * @param int $height 高 */ function createVerCode($type = 1, $leng = 4, $pixel = 0, $line = 0, $arc = 0, $snow = 0, $width = 100, $height = 20) { //创建画布 $image = imagecreatetruecolor($width, $height);//创建画布,返回资源 //创建颜色 $white = imagecolorallocate($image, 255, 255, 255); //绘制填充矩形,用特定颜色填充画布 imagefilledrectangle($image, 0, 0, $width, $height, $white); if ($leng <= 3) $leng = 3; if ($leng >= 6) $leng = 6; $verCodeString = ''; //绘制复杂点的验证码 for ($i = 0; $i < $leng; $i++) { $verCode = randChar($type); $verCodeString .= $verCode; //生成的验证码 $x = 10 + ($width / $leng) * $i; $y = 0; imagechar($image, 10, $x, $y, $verCode, randColor($image)); } //将验证码存入session session_start(); $_SESSION['verCodeString'] = $verCodeString; //加随机点做干扰元素 if ($pixel > 0) { for ($i = 1; $i <= $pixel; $i++) { imagesetpixel($image, mt_rand(0, $width), mt_rand(0, $height), randColor($image)); } } //加线段做干扰元素 if ($line > 0) { for ($i = 1; $i <= $line; $i++) { imageline($image, mt_rand(0, $width), mt_rand(0, $height), mt_rand(0, $width), mt_rand(0, $height), randColor($image)); } } //加圆弧做干扰元素 if ($arc > 0) { for ($i = 1; $i <= $arc; $i++) { imagearc($image, mt_rand(0, $width), mt_rand(0, $height), mt_rand(0, $width / 2), mt_rand(0, $height / 2), mt_rand(0, 360), mt_rand(0, 360), randColor($image)); } } //加雪花做干扰元素 if ($snow > 0) { for ($i = 1; $i <= $snow; $i++) { imagestring($image, mt_rand(0,5),mt_rand(0, $width), mt_rand(0, $height),'*',randColor($image)); } } //告诉浏览器以图片形式显示 header('Content-type: image/png');//png //输出图像 imagepng($image);//png //销毁资源 imagedestroy($image); } /** * 返回随机颜色 * 图片资源 $image * 颜色 int */ function randColor($image) { return imagecolorallocate($image, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255)); } /** * 返回一个字符 * @param int $type 1数字 2字母 3数字或字母 * @return string */ function randChar($type = 1) { if ($type == 1) { return str_shuffle(implode('', range(0, 9)))[0]; } if ($type == 2) { return str_shuffle(implode('', array_merge(range('a', 'z'), range('A', "Z"))))[0]; } if ($type == 3) { return str_shuffle(implode('', array_merge(range(0, 9), range('a', 'z'), range('A', "Z"))))[0]; } return ''; } createVerCode(1, 4, 0, 0,0,5);
2.验证码类
<?php /** * 验证码类 * Created by PhpStorm. * User: sxt * Date: 2019/12/13 * Time: 11:12 */ class verCode{ private $type; private $leng; private $pixel; private $line; private $arc; private $snow; private $width; private $height; private $white; private $image = null; private $verCodeString = ''; private $verCode = ''; public function __construct($type = 1, $leng = 4, $pixel = 0, $line = 0, $arc = 0, $snow = 0, $width = 100, $height = 20) { $this->type = $type; $this->leng = $leng; $this->pixel = $pixel; $this->line = $line; $this->arc = $arc; $this->snow = $snow ; $this->width = $width; $this->height = $height; } public function getVerCode() { //创建画布 $this->image = imagecreatetruecolor($this->width, $this->height);//创建画布,返回资源 //创建颜色 $this->white = imagecolorallocate($this->image, 255, 255, 255); //绘制填充矩形,用特定颜色填充画布 imagefilledrectangle($this->image, 0, 0, $this->width, $this->height, $this->white); if ($this->leng <= 3) $this->leng = 3; if ($this->leng >= 6) $this->leng = 6; //绘制复杂点的验证码 for ($i = 0; $i < $this->leng; $i++) { $this->verCode = $this->randChar(); $this->verCodeString .= $this->verCode; //生成的验证码 imagechar($this->image, 10, 10 + ($this->width / $this->leng) * $i, 0, $this->verCode, $this->randColor()); } //加随机点做干扰元素 if ($this->pixel > 0) { $this->createPixel(); } //加线段做干扰元素 if ($this->line > 0) { $this->createLine(); } //加圆弧做干扰元素 if ($this->arc > 0) { $this->createArc(); } //加雪花做干扰元素 if ($this->snow > 0) { $this->createSnow(); } header('Content-type: image/png');//png imagepng($this->image);//png imagedestroy($this->image); //return $this->verCodeString; } /** * 生成干扰点 */ private function createPixel(){ for ($i = 1; $i <= $this->pixel; $i++) { imagesetpixel($this->image, mt_rand(0, $this->width), mt_rand(0, $this->height), $this->randColor()); } } /** * 生成干扰直线 */ private function createLine(){ for ($i = 1; $i <= $this->line; $i++) { imageline($this->image, mt_rand(0, $this->width), mt_rand(0, $this->height), mt_rand(0, $this->width), mt_rand(0, $this->height), $this->randColor()); } } /** * 生成干扰圆弧 */ private function createArc (){ for ($i = 1; $i <= $this->arc; $i++) { imagearc($this->image, mt_rand(0, $this->width), mt_rand(0, $this->height), mt_rand(0, $this->width / 2), mt_rand(0, $this->height / 2), mt_rand(0, 360), mt_rand(0, 360), $this->randColor()); } } /** * 生成干扰雪花 */ private function createSnow(){ for ($i = 1; $i <= $this->snow; $i++) { imagestring($this->image, mt_rand(0,5),mt_rand(0, $this->width), mt_rand(0, $this->height),'*',$this->randColor()); } } /** * 返回随机颜色 */ private function randColor() { return imagecolorallocate($this->image, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255)); } /** * 返回一个字符 * @param int $type 1数字 2字母 3数字或字母 * @return string */ private function randChar() { if ($this->type == 1) { return str_shuffle(implode('', range(0, 9)))[0]; } if ($this->type == 2) { return str_shuffle(implode('', array_merge(range('a', 'z'), range('A', "Z"))))[0]; } if ($this->type == 3) { return str_shuffle(implode('', array_merge(range(0, 9), range('a', 'z'), range('A', "Z"))))[0]; } return ''; } }
3.显示表单页面
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>验证码</title> </head> <body> <form action="checkVerCode.php" method="post"> <table> <tr> <td>用户名</td> <td><input type="text" name="name"></td> </tr> <tr> <td>密码</td> <td><input type="text" name="pass"></td> </tr> <tr> <td>验证码</td> <td><input type="text" name="vercode"> <img src="verCodefun.php" id ='verCodefun'> <a href="#" οnclick="document.getElementById('verCodefun').src='verCodefun.php?r='+Math.random()"> 看不清,换一张 </a> </td> </tr> <tr> <td> <input type="submit" value="提交"> </td> </tr> </table> </form> <!--图片验证码--> <!--<form action="checkVerCode.php" method="post">--> <!--<table>--> <!--<tr>--> <!--<td>用户名</td>--> <!--<td><input type="text" name="name"></td>--> <!--</tr>--> <!--<tr>--> <!--<td>密码</td>--> <!--<td><input type="text" name="pass"></td>--> <!--</tr>--> <!--<tr>--> <!--<td>图片里的动物是</td>--> <!--<td><input type="text" name="vercode">--> <!--<img src="imageVerCode.php" id ='verCodefun' width="100" height="100">--> <!--<a href="#" οnclick="document.getElementById('verCodefun').src='imageVerCode.php?r='+Math.random()">--> <!--看不清,换一张--> <!--</a>--> <!--</td>--> <!--</tr>--> <!--<tr>--> <!--<td>--> <!--<input type="submit" value="提交">--> <!--</td>--> <!--</tr>--> <!--</table>--> <!--</form>--> </body> </html>
4.验证码检验
<?php session_start(); //$verCodeString = $_SESSION['verCodeString'];//验证一般验证码 $verCodeString = $_SESSION['imageCode'];//验证图片验证码 if ($verCodeString != $_POST['vercode']) { exit('验证码错误'); }else{ exit('验证码正确'); }
5.扩展的图片验证码
<?php //图片与汉字对应关系 $array =[ '0'=>'狗', '1'=>'鹿', '2'=>'狼', '3'=>'熊猫', '4'=>'猫', ]; $rand = mt_rand(0, 4); //保存 session_start(); $_SESSION['imageCode'] = $array[$rand]; //输出图像 $contents = file_get_contents("../image/$rand.jpeg"); header('content-type:image/jpeg'); echo $contents;