💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
[TOC] 自己写的验证码是这几个文件 Captch.class.php(验证码工具类) home.html(前台页面,入口文件) check.php(检查处理文件) # Captcha.class.php ~~~ <?php class Captcha{ //码值库 private $charset =<<<"herdoc" abcdefghkmnprstuvwxyzabcdefghkmnprstuvwxyzABCDEFGHKMNPRSTUVWXYZ01234567890123456789 lol英雄如下 战争女神 希维尔 轮子妈 曙光女神 蕾欧娜 风暴女神 迦娜 皎月女神 黛安娜 朋友圈都是卖面膜 真的好烦 herdoc; private $code; //验证码字符串 private $codelen = 4; //验证码长度 private $width = 150; //宽度 private $height = 50; //高度 private $img; //图形资源句柄 private $font; //指定的字体 private $fontsize = 20; //指定字体大小 private $fontcolor; //指定字体颜色 //构造方法初始化 /** * @param $codelen=4,$width=150,$height=40,$fontsize = 20,$font="Elephant.ttf" * @access public */ public function __construct($codelen=4,$width=150,$height=40,$fontsize = 20,$font="./simkai.ttf") { $this->charset = str_replace(" ", "x", $this->charset); $this->charset = str_replace("\n", "j", $this->charset); $this->charset = str_replace("\r", "d", $this->charset); $this->charset = str_replace("\t", "z", $this->charset); $this->codelen = $codelen; $this->width = $width; $this->height = $height; $this->fontsize = $fontsize; $this->font = $font; } /** * 生成随机码,也就是码值,保存到$this->code * @access private * @return void */ private function createCode() { $_len = mb_strlen($this->charset,'utf-8')-1; $this->code=""; //初始化码值字符串 for($i=0;$i<$this->codelen;++$i){ $rand_index = mt_rand(0,$_len); $this->code .= mb_substr($this->charset,$rand_index,1,'utf-8'); } /* for ($i=0;$i<$this->codelen;++$i) { $this->code .= mb_substr($this->charset,mt_rand(0,$_len),1,'utf-8'); } */ } /** * 生成背景色 * @access private * @return void */ private function createBg() { $this->img = imagecreatetruecolor($this->width, $this->height); $color = imagecolorallocate($this->img, mt_rand(170,250), mt_rand(170,255), mt_rand(170,255)); imagefilledrectangle($this->img,0,$this->height,$this->width,0,$color); } /** * 生成文字到画布 * @access private * @return void */ private function createFont() { $_x = $this->width / $this->codelen; for ($i=0;$i<$this->codelen;++$i) { $this->fontcolor = imagecolorallocate($this->img,mt_rand(0,156),mt_rand(0,160),mt_rand(0,160));; imagettftext($this->img,$this->fontsize,mt_rand(-30,50),$_x*$i+mt_rand(1,5),$this->height / 1.4,$this->fontcolor,$this->font,mb_substr($this->code,$i,1,'utf-8')); } } /** * 生成线条、雪花 * @access private * @return void */ private function createLine() { for ($i=0;$i<6;++$i) { $color = imagecolorallocate($this->img,mt_rand(0,156),mt_rand(0,156),mt_rand(0,156)); imageline($this->img,mt_rand(0,$this->width),mt_rand(0,$this->height),mt_rand(0,$this->width),mt_rand(0,$this->height),$color); } for ($i=0;$i<100;++$i) { $color = imagecolorallocate($this->img,mt_rand(150,200),mt_rand(200,255),mt_rand(150,255)); imagestring($this->img,mt_rand(1,5),mt_rand(0,$this->width),mt_rand(0,$this->height),'*',$color); } //点 for($k=0;$k<300;$k++){ $color=imagecolorallocate($this->img,mt_rand(0,200),mt_rand(0,200),mt_rand(0,200)); imagesetpixel($this->img,mt_rand(1,$this->width-2),mt_rand(1,$this->height-2),$color); } //圆弧 for($a=0;$a<20;$a++){ $color=imagecolorallocate($this->img,mt_rand(0,200),mt_rand(0,200),mt_rand(0,200)); imagearc($this->img,mt_rand(10,$this->width-10),mt_rand(4,$this->height-5),mt_rand(20,100),mt_rand(20,100),20,100,$color); } } /** * 输出到网页 * @access private */ private function outPut() { ob_end_clean(); //清空前面的输出 if(imagetypes()&IMG_GIF){ header("Content-Type:image/gif"); imagegif($this->img); }elseif(imagetypes()&IMG_PNG){ header("Content-Type:image/png"); imagepng($this->img); }elseif(imagetypes()&IMG_JPEG){ header("Content-Type:image/jpge"); imagejpeg($this->img,null,100); }else{ die("error:no image in the php server"); } imagedestroy($this->img); } /** * 对外生成 * @access private * @return 验证码 */ public function generateCode() { $this->createBg(); $this->createCode(); $this->createLine(); $this->createFont(); $this->outPut(); } /** * 获取验证码的正确码值 * @access public * @return string 小写的验证码码值 */ public function getCode() { return strtolower($this->code); } } $c = new Captcha(); $c->generateCode(); @session_start(); $_SESSION['captcha'] = $c->getCode(); ~~~ # home.html ~~~ <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>session 图片验证实例</title> <style type="text/css"> #login p{ margin-top: 15px; line-height: 20px; font-size: 14px; font-weight: bold; } #login img{ cursor:pointer; } form{ margin-left:20px; } </style> </head> <body> <?php @session_start(); header('Expires:-1'); header('Cache-Control:no-cache'); header('Pragma:no-cache'); ?> <form id="login" action="./check.php" method="post"> <p>此例为session验证码,不区分大小写,点击图片更换</p> <p> <span>验证码:</span> <input type="text" name="validate" value="" size=10> <img title="点击刷新" src="Captcha.class.php" onclick="this.src='Captcha.class.php?r='+Math.random()"></img> </p> <p> <input type="submit"> </p> </form> ~~~ # check.php ~~~ <?php @session_start(); echo "<a href='./home.html'>返回验证</a><br /><br />"; if(isset($_POST["validate"])){ $validate=mb_strtolower(trim($_POST["validate"]),'utf-8'); echo "您刚才输入的是!<font color='blue'>".$_POST["validate"]."</font>!<br>"; if($validate != $_SESSION["captcha"]){ //判断session值与用户输入的验证码是否一致; echo "<font color=red>输入有误</font>"; echo "<br />答案是!<font color='blue'>".$_SESSION['captcha'],"</font>!<br />"; }else{ echo "<font color=green>通过验证</font>"; echo "<br />答案是!<font color='blue'>".$_SESSION['captcha'],"</font>!<br />"; } } ?> ~~~