
php教程--案例25-2(验证码生成与验证(线条))
发布日期:2021-05-06 21:10:29
浏览次数:16
分类:原创文章
本文共 2354 字,大约阅读时间需要 7 分钟。
t214.php
<html><head> <title>验证码生成与验证</title> <script type="text/javascript"> function change1() { var change = document.getElementById("change"); var img = document.getElementById("code_img"); img.src = "./t215.php?t="+ new Date(); } </script></head><body><form action="./t214.php" method="post"> <p>用户名:<input type="text" name="user_name" value=""></p> <p>密码:<input type="password" name="password" value=""></p> <p>验证码:<input type="text" name="captcha"><img id="code_img" src="./t215.php"><a href="#" onclick="change1();">看不清,换一张</a></p> <p><input type="submit" name="login" value="登录" style="width: 180px;"></p></form></body></html><?php//case 25-2 验证码生成与验证(线条)@session_start();if(isset($_POST['login'])){ $code = isset($_POST['captcha'])?trim($_POST['captcha']):''; if(!isset($_SESSION['captcha_code'])) die('验证码已过期,请重新登录'); echo "code=$code"."<br>"; echo "session =".$_SESSION['captcha_code']."<br>"; if(strtolower($code) == strtolower($_SESSION['captcha_code'])) { echo "验证码正确"; } else { echo "验证码输入错误"; } session_destroy();}?>
t215.php
<?php$img_width = 120;$img_height = 35;$char_len = 5;$font = 5;$char = array_merge(range('A','Z'),range('a','z'),range(1,9));$rand_keys = array_rand($char,$char_len);if($char_len == 1){ $rand_keys = array($rand_keys);}shuffle($rand_keys);$code = '';foreach ($rand_keys as $key){ $code .= $char[$key];}@session_start();$_SESSION['captcha_code'] = $code;$img = imagecreatetruecolor($img_width,$img_height);$bg_color = imagecolorallocate($img,0xcc,0xcc,0xcc);imagefill($img,0,0,$bg_color);//线条for ($i=0; $i<=6; $i++){ $color = imagecolorallocate($img,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255)); imageline($img,mt_rand(0,$img_width),mt_rand(0,$img_height),mt_rand(0,$img_width),mt_rand(0,$img_height),$color);}$rect_color = imagecolorallocate($img,0xff,0xff,0xff);imagerectangle($img,0,0,$img_width-1,$img_height-1,$rect_color);$str_color = imagecolorallocate($img,mt_rand(0,100),mt_rand(0,100),mt_rand(0,100));$font_width = imagefontwidth($font);$font_height = imagefontheight($font);$str_width = $font_width * $char_len;imagestring($img,$font,($img_width-$str_width)/2,($img_height-$font_height)/2,$code,$str_color);header('Content-type:image/png');imagepng($img);imagedestroy($img);
发表评论
最新留言
感谢大佬
[***.8.128.20]2025年03月22日 06时08分31秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
java ==和equals
2019-03-04
java 接口(Interface)多态特性
2019-03-04
搜集整理随机产生人的姓名的2种方法
2019-03-04
最简单的Socket程序[入门篇]
2019-03-04
VS2005图标默认存放位置
2019-03-04
常用正则表达式
2019-03-04
C#中换行的代码
2019-03-04
用正则表达式过滤多余空格
2019-03-04
XML:采用XHTML和CSS设计可重用可换肤的WEB站点
2019-03-04
U盘“无法识别的USB设备”解决办法
2019-03-04
python入门——运算符
2019-03-04
【springmvc】传值的几种方式&&postman接口测试
2019-03-04
泳道图简介
2019-03-04
Tomcat6中web项目部署路径webapps和wtpwebapps的区别
2019-03-04
Java判断字符串是否为金额
2019-03-04
CodeCombat代码全记录(Python学习利器)--安息之云山峰(第四章)代码9
2019-03-04
nginx配置文件nginx.conf详细讲解(2)
2019-03-04
nginx配置文件nginx.conf详细讲解(4)--终结篇
2019-03-04
某公司运维岗位笔试题8
2019-03-04