博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
php 验证码
阅读量:7060 次
发布时间:2019-06-28

本文共 4053 字,大约阅读时间需要 13 分钟。

学习地址:http://www.imooc.com/video/2458

strlen ( $chars )) { exit ( "字符串长度不够" ); } $chars = str_shuffle ( $chars ); return substr ( $chars, 0, $length );}//通过GD库做验证码function verifyImage($type=1,$length=4,$pixel=0,$line=0,$sess_name = "verify"){ session_start(); //创建画布 $width = 80; $height = 28; $image = imagecreatetruecolor ( $width, $height ); $white = imagecolorallocate ( $image, 255, 255, 255 ); $black = imagecolorallocate ( $image, 0, 0, 0 ); //用填充矩形填充画布 imagefilledrectangle ( $image, 1, 1, $width - 2, $height - 2, $white ); $chars = buildRandomString ( $type, $length ); $_SESSION [$sess_name] = $chars; //$fontfiles = array ("MSYH.TTF", "MSYHBD.TTF", "SIMLI.TTF", "SIMSUN.TTC", "SIMYOU.TTF", "STZHONGS.TTF" ); $fontfiles = array ("SIMYOU.TTF" ); //由于字体文件比较大,就只保留一个字体,如果有需要的同学可以自己添加字体,字体在你的电脑中的fonts文件夹里有,直接运行输入fonts就能看到相应字体 for($i = 0; $i < $length; $i ++) { $size = mt_rand ( 14, 18 ); $angle = mt_rand ( - 15, 15 ); $x = 5 + $i * $size; $y = mt_rand ( 20, 26 ); $fontfile = "../fonts/" . $fontfiles [mt_rand ( 0, count ( $fontfiles ) - 1 )]; $color = imagecolorallocate ( $image, mt_rand ( 50, 90 ), mt_rand ( 80, 200 ), mt_rand ( 90, 180 ) ); $text = substr ( $chars, $i, 1 ); imagettftext($image, $size, $angle, $x, $y, $color, "", $text); } if ($pixel) { for($i = 0; $i < 50; $i ++) { imagesetpixel ( $image, mt_rand ( 0, $width - 1 ), mt_rand ( 0, $height - 1 ), $black ); } } if ($line) { for($i = 1; $i < $line; $i ++) { $color = imagecolorallocate ( $image, mt_rand ( 50, 90 ), mt_rand ( 80, 200 ), mt_rand ( 90, 180 ) ); imageline ( $image, mt_rand ( 0, $width - 1 ), mt_rand ( 0, $height - 1 ), mt_rand ( 0, $width - 1 ), mt_rand ( 0, $height - 1 ), $color ); } } header ( "content-type:image/gif" ); imagegif ( $image ); imagedestroy ( $image );}

 

网上找到的不错的验证码:http://www.jb51.net/article/40341.htm

需要下载字体:elephant.ttf

html使用:<img  title="点击刷新" src="./captcha.php" align="absbottom" οnclick="this.src='captcha.php?'+Math.random();"></img>

font = dirname(__FILE__).'/font/elephant.ttf';//注意字体路径要写对,否则显示不了图片 } //生成随机码 private function createCode() { $_len = strlen($this->charset)-1; for ($i=0;$i<$this->codelen;$i++) { $this->code .= $this->charset[mt_rand(0,$_len)]; } } //生成背景 private function createBg() { $this->img = imagecreatetruecolor($this->width, $this->height); $color = imagecolorallocate($this->img, mt_rand(157,255), mt_rand(157,255), mt_rand(157,255)); imagefilledrectangle($this->img,0,$this->height,$this->width,0,$color); } //生成文字 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,156),mt_rand(0,156)); imagettftext($this->img,$this->fontsize,mt_rand(-30,30),$_x*$i+mt_rand(1,5),$this->height / 1.4,$this->fontcolor,$this->font,$this->code[$i]); } } //生成线条、雪花 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(200,255),mt_rand(200,255),mt_rand(200,255)); imagestring($this->img,mt_rand(1,5),mt_rand(0,$this->width),mt_rand(0,$this->height),'*',$color); } } //输出 private function outPut() { header('Content-type:image/png'); imagepng($this->img); imagedestroy($this->img); } //对外生成 public function doimg() { $this->createBg(); $this->createCode(); $this->createLine(); $this->createFont(); $this->outPut(); } //获取验证码 public function getCode() { return strtolower($this->code); }}//使用方法,通常是在另一个单独的php页面执行的:/*$_vc = new ValidateCode(); $_vc->doimg(); $_SESSION['ValidateCode'] = $_vc->getCode();//验证码保存到SESSION中*///然后在html中引用php页面:/**/?>

 

转载地址:http://mzfll.baihongyu.com/

你可能感兴趣的文章
postgresSQL 实现数据修改后,自动更新updated_date/ts等字段
查看>>
老黄历接口(免注册)
查看>>
移动端开发适配总结
查看>>
CSS3阴影 box-shadow的使用和技巧总结
查看>>
RAC下修改SGA的实战操作
查看>>
JQuery/AjaX/Javascript/DIV+CSS资源下载地址
查看>>
linux下使用lftp的小结
查看>>
jqGrid的若干种用法
查看>>
jQuery实现文本框回车键转tab键 分类: JavaScript ...
查看>>
内存程序文件、内存对齐程序
查看>>
wp7设置浏览器主页
查看>>
资源管理更新系统V2.0版的一些问题
查看>>
Sil“.NET研究”verlight与HTML双向交互
查看>>
More-iOS中的Ping
查看>>
React 重要的一次重构:认识异步渲染架构 Fiber
查看>>
TensorFlow笔记(2)——利用TensorFlow训练一个最简单的一元线性模型
查看>>
TensorFlow笔记(4)——优化手写数字识别模型之代价函数和拟合
查看>>
微服务java_b2b商城系统_java商城源码100%开源适合2次开发-(七)高可用的分布式配置中心(Spring Cloud Config)...
查看>>
Swift5.0新特性更新
查看>>
React Redux 中间件思想遇见 Web Worker 的灵感(附demo)
查看>>