一段实用的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 相关文章推荐
一个颜色轮换的简单例子
Oct 09 PHP
php预定义常量
Dec 25 PHP
php获取url字符串截取路径的文件名和扩展名的函数
Jan 22 PHP
解析php中的escape函数
Jun 29 PHP
PHP自动生成后台导航网址的最佳方法
Aug 27 PHP
php使用Cookie控制访问授权的方法
Jan 21 PHP
详解PHP的Yii框架中自带的前端资源包的使用
Mar 31 PHP
Yii2中YiiBase自动加载类、引用文件方法分析(autoload)
Jul 25 PHP
PHP获取不了React Native Fecth参数的解决办法
Aug 26 PHP
yii2 数据库读写分离配置示例
Feb 10 PHP
PHP Class SoapClient not found解决方法
Jan 20 PHP
PHP yield关键字功能与用法分析
Jan 03 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网站备份程序代码分享
2011/06/10 PHP
PHP批量删除、清除UTF-8文件BOM头的代码实例
2014/04/14 PHP
使用配置类定义Codeigniter全局变量
2014/06/12 PHP
人脸识别测颜值、测脸龄、测相似度微信接口
2016/04/07 PHP
PHP实现倒计时功能
2020/11/16 PHP
TP5多入口设置实例讲解
2020/12/15 PHP
在Javascript中定义对象类别
2006/12/22 Javascript
javascript 学习之旅 (2)
2009/02/05 Javascript
FireFox下XML对象转化成字符串的解决方法
2011/12/09 Javascript
『JavaScript』限制Input只能输入数字实现思路及代码
2013/04/22 Javascript
js hover 定时器(实例代码)
2013/11/12 Javascript
js实现的点击div区域外隐藏div区域
2014/06/30 Javascript
详解JS函数重载
2014/12/04 Javascript
JavaScript知识点总结之如何提高性能
2016/01/15 Javascript
JS图片压缩(pc端和移动端都适用)
2017/01/12 Javascript
JavaScript正则替换HTML标签功能示例
2017/03/02 Javascript
jQuery选择器中的特殊符号处理方法
2017/09/08 jQuery
浅谈vue中改elementUI默认样式引发的static与assets的区别
2018/02/03 Javascript
vue组件横向树实现代码
2018/08/02 Javascript
简介Python中用于处理字符串的center()方法
2015/05/18 Python
在Python中处理字符串之isdigit()方法的使用
2015/05/18 Python
在CentOS6上安装Python2.7的解决方法
2018/01/09 Python
对pandas进行数据预处理的实例讲解
2018/04/20 Python
python 实现A*算法的示例代码
2018/08/13 Python
解决python 上传图片限制格式问题
2019/10/30 Python
Python通过kerberos安全认证操作kafka方式
2020/06/06 Python
python软件都是免费的吗
2020/06/18 Python
25个CSS3动画按钮和菜单教程分享
2012/10/03 HTML / CSS
自考毕业生自我鉴定
2013/11/04 职场文书
表演方阵解说词
2014/02/08 职场文书
妇联领导班子剖析材料
2014/08/21 职场文书
优秀大学生事迹材料
2014/12/24 职场文书
奖励通知
2015/04/22 职场文书
2015年财务个人工作总结范文
2015/05/22 职场文书
mybatis 解决从列名到属性名的自动映射失败问题
2021/06/30 Java/Android
教你使用RustDesk 搭建一个自己的远程桌面中继服务器
2022/08/14 Servers