一段实用的php验证码函数


Posted in PHP onMay 19, 2016

本文实例为大家分享了几段php验证码函数,都很实用,具体内容如下

代码段一:简单php验证码函数

<?php
 
 function code(){
 
 $im = imagecreatetruecolor(100, 40);
 
 $black = imagecolorallocate($im, 0, 0, 0);
 
 $white = imagecolorallocate($im, 255, 255, 255);
 
 
 
 imagefill($im,0,0,$white);
 
 $strarr=array_merge(range(0,9),range(a,z),range(A,Z));
 
 shuffle($strarr);
 
 $str=join(array_slice($strarr,0,4));
 
 $mm=rand(0,40);
 
 $aaa=rand(0,0);
 
 $bbb=rand(0,255);
 
 $ccc=rand(0,255);
 
 $color1=imagecolorallocate($im,$aaa,$bbb,$ccc);
 
 imagestring($im,5,$mm,10,$str,$color1);
 
 for($i=0;$i<700;$i++){
 
   $aa=rand(0,255);
 
   $bb=rand(0,255);
 
   $cc=rand(0,255);
 
 $color=imagecolorallocate($im,$aa,$bb,$cc);
 
   $a=rand(0,100);
 
   $b=rand(0,40);
 
   imagesetpixel($im,$a,$b,$color);
 
 }
 
 for($t=0;$t<3;$t++){
 
   $a1=rand(0,255);
 
   $b1=rand(0,255);
 
   $c1=rand(0,255);
 
 $color2=imagecolorallocate($im,$a1,$b1,$c1);
 
   $a2=rand(0,100);
 
   $b2=rand(0,40);
 
   $a3=rand(0,100);
 
   $b3=rand(0,40);
 
   imageline($im,$a2,$b2,$b3,$b3,$color2);
 
 }
 
 
 
 header("content-type:image/png");
 
 imagepng($im);
 
 }
 
 code() ;
 
?>

代码段二:php验证码函数

function _code($_code_length = 4, $_width = 75, $_height = 25){
 for($i=0;$i<$_code_length;$i++){
  $_nmsg .= dechex(mt_rand(0,15));
 }
 $_SESSION["code"] = $_nmsg;

 $_img = imagecreatetruecolor($_width, $_height);

 $_white = imagecolorallocate($_img, 250, 250, 250);

 imagefill($_img, 0, 0, $_white);

 $_gray = imagecolorallocate($_img, 196, 196, 196);

 imagerectangle($_img, 0, 0, $_width-1, $_height-1, $_gray);

 for ($i=0; $i < 6; $i++) { 
  $_md_color = imagecolorallocate($_img, mt_rand(200,255), mt_rand(200,255), mt_rand(200,255));
  imageline($_img, mt_rand(0,$_width), mt_rand(0, $_height),mt_rand(0,$_width), mt_rand(0, $_height), $_md_color);
 }

 for ($i=0; $i < 50; $i++) { 
  $_md_color = imagecolorallocate($_img, mt_rand(200,255), mt_rand(200,255), mt_rand(200,255));
  imagestring($_img, 1, mt_rand(1,$_width-5), mt_rand(1, $_height-5), "*", $_md_color);
 }

 for ($i=0; $i < $_code_length ; $i++) { 
  $_md_color = imagecolorallocate($_img, mt_rand(0,102), mt_rand(0,102), mt_rand(0,102));
  imagestring($_img, 5, $i * $_width/$_code_length+ mt_rand(1, 10), mt_rand(1, $_height/2), $_SESSION["code"][$i], $_md_color);
 }

 header("Content-Type:image/png");

 imagepng($_img);

 imagedestroy($_img);
}

代码段三:php图片验证码函数

/*@ captcha()函数的功能:生成验证码
 * @ 可自定义参数:
 * @ $width 图片宽度,默认80
 * @ $high 高度,默认25
 * @ $num 验证码个数,默认4个
 * @ $line_num 随机画线条的个数,默认10
 * @ $snow_num 随机雪花的数量,默认50
 */
function captcha($width=80,$high=25,$num=4,$line_num=10,$snow_num=50){
 header('Content-Type:image/png');
 session_start();
 //生成随机数字+字母
 for($a = 0;$a < $num;$a++){
  $code .= dechex(mt_rand(0, 15));//dechex — 十进制转换为十六进制
 }
 //把生成的验证码保存在SESSION超级全局数组中
 $_SESSION['captcha'] = $code;
 //创建画布
 $img = imagecreatetruecolor($width,$high);
 //填充背景色为白色
 $backcolor = imagecolorallocate($img, '255', '255', '255');
 imagefill($img, '0', '0', $backcolor);
 //添加黑色边框
 $bordercolor = imagecolorallocate($img, 0, 0, 0);
 imagerectangle($img, 0, 0, $width-1, $high-1, $bordercolor);
 //随机画线条
 for($i=0;$i<$line_num;$i++){
  imageline($img, mt_rand(0, $width*0.1), mt_rand(0, $high), mt_rand($width*0.9, $width), mt_rand(0, $high),
  imagecolorallocate($img, mt_rand(150, 255), mt_rand(150, 255), mt_rand(150, 255)));
 }
 //随机打雪花
 for ($i=0;$i<$snow_num;$i++){
  imagechar($img,1, mt_rand(0, $width), mt_rand(0, $high),'*',
  imagecolorallocate($img, mt_rand(200, 255), mt_rand(200, 255), mt_rand(200, 255)));
 }
 //画验证码
 for ($b = 0;$b < strlen($_SESSION['captcha']);$b++){
  imagechar($img,5, $b*$width/$num+mt_rand(5,10), mt_rand(2, $high/2),$_SESSION['captcha'][$b],
  imagecolorallocate($img, mt_rand(10, 150), mt_rand(10, 150), mt_rand(0, 100)));
 }
 ob_clean();//清空输出缓冲区
 imagepng($img);
 imagedestroy($img);
}

以上就是三段参考性较高的php验证码函数,希望对大家学习php程序设计有所帮助。

PHP 相关文章推荐
手把手教你使用DedeCms的采集的图文教程
Mar 11 PHP
Base64在线编码解码实现代码 演示与下载
Jan 08 PHP
用PHP编写和读取XML的几种方式
Jan 12 PHP
php 获取页面中指定内容的实现类
Jan 23 PHP
php循环创建目录示例分享(php创建多级目录)
Mar 04 PHP
php网页版聊天软件实现代码
Aug 12 PHP
php+jquery+html实现点击不刷新加载更多的实例代码
Aug 12 PHP
注意!PHP 7中不要做的10件事
Sep 18 PHP
老生常谈文本文件和二进制文件的区别
Feb 27 PHP
Laravel如何创建服务器提供者实例代码
Apr 15 PHP
自定义Laravel (monolog)日志位置,并增加请求ID的实现
Oct 17 PHP
PHP连续签到功能实现方法详解
Dec 04 PHP
thinkphp3.x中cookie方法的用法分析
May 19 #PHP
thinkphp3.x中display方法及show方法的用法实例
May 19 #PHP
thinkphp3.x连接mysql数据库的方法(具体操作步骤)
May 19 #PHP
thinkphp3.x自定义Action、Model及View的简单实现方法
May 19 #PHP
thinkPHP实现递归循环栏目并按照树形结构无限极输出的方法
May 19 #PHP
php处理json格式数据经典案例总结
May 19 #PHP
CI框架整合smarty步骤详解
May 19 #PHP
You might like
php中转义mysql语句的实现代码
2011/06/24 PHP
PHP中抽象类、接口的区别与选择分析
2016/03/29 PHP
Zend Framework入门教程之Zend_Registry组件用法详解
2016/12/09 PHP
PHP实现的数独求解问题示例
2017/04/18 PHP
PHP 对象继承原理与简单用法示例
2020/04/21 PHP
各浏览器对link标签onload/onreadystatechange事件支持的差异分析
2011/04/27 Javascript
深入理解JavaScript系列(4) 立即调用的函数表达式
2012/01/15 Javascript
jquery 循环显示div的示例代码
2013/10/18 Javascript
JavaScript模块随意拖动示例代码
2014/05/27 Javascript
Jquery实现动态切换图片的方法
2015/05/18 Javascript
bootstrap table操作技巧分享
2017/02/15 Javascript
ztree实现左边动态生成树右边为内容详情功能
2017/11/03 Javascript
快速处理vue渲染前的显示问题
2018/03/05 Javascript
js中的 || 与 &amp;&amp; 运算符详解
2018/05/24 Javascript
NodeJs项目中关闭ESLint的方法
2018/08/09 NodeJs
JS Array.from()将伪数组转换成数组的方法示例
2020/03/23 Javascript
[51:11]2014 DOTA2国际邀请赛中国区预选赛5.21 LGD-CDEC VS DT
2014/05/22 DOTA
[03:14]辉夜杯主赛事 12月25日每日之星
2015/12/26 DOTA
Python基于递归算法实现的走迷宫问题
2017/08/04 Python
Python设置在shell脚本中自动补全功能的方法
2018/06/25 Python
Python 存储字符串时节省空间的方法
2019/04/23 Python
python多继承(钻石继承)问题和解决方法简单示例
2019/10/21 Python
python 解决cv2绘制中文乱码问题
2019/12/23 Python
python随机模块random的22种函数(小结)
2020/05/15 Python
澳大利亚领先的在线礼品网站:Gifts Australia
2020/08/15 全球购物
小学信息技术教学反思
2014/02/10 职场文书
文秘求职信范文
2014/04/10 职场文书
爱国演讲稿500字
2014/05/04 职场文书
公司节能减排倡议书
2014/05/14 职场文书
美术第二课堂活动总结
2014/07/08 职场文书
2014入党积极分子批评与自我批评思想报告
2014/10/06 职场文书
2014年财务工作总结范文
2014/11/11 职场文书
考试没考好检讨书(精选篇)
2014/11/16 职场文书
Linux安装apache服务器的配置过程
2021/11/27 Servers
MySQL创建管理HASH分区
2022/04/13 MySQL
MySQL详细讲解变量variables的用法
2022/06/21 MySQL