关于php Captcha 驗證碼類的讲解
<?php /** Captcha 驗證碼類 * Date: 2011-02-19 * Author: fdipzone */ class Captcha{ //class start private $sname = ''; public function __construct($sname=''){ // $sname captcha session name $this->sname = $sname==''? 'm_captcha' : $sname; } /** 生成验证码图片 * @param int $length 驗證碼長度 * @param Array $param 參數 * @return IMG */ public function create($length=4,$param=array()){ Header("Content-type: image/PNG"); $authnum = $this->random($length); //生成验证码字符. $width = isset($param['width'])? $param['width'] : 13; //文字宽度 $height = isset($param['height'])? $param['height'] : 18; //文字高度 $pnum = isset($param['pnum'])? $param['pnum'] : 100; //干扰象素个数 $lnum = isset($param['lnum'])? $param['lnum'] : 2; //干扰线条数 $this->captcha_session($this->sname,$authnum); //將隨機數寫入session $pw = $width*$length+10; $ph = $height+6; $im = imagecreate($pw,$ph); //imagecreate() 新建图像,大小为 x_size 和 y_size 的空白图像。 $black = ImageColorAllocate($im, 238,238,238); //设置背景颜色 $values = array( mt_rand(0,$pw), mt_rand(0,$ph), mt_rand(0,$pw), mt_rand(0,$ph), mt_rand(0,$pw), mt_rand(0,$ph), mt_rand(0,$pw), mt_rand(0,$ph), mt_rand(0,$pw), mt_rand(0,$ph), mt_rand(0,$pw), mt_rand(0,$ph) ); imagefilledpolygon($im, $values, 6, ImageColorAllocate($im, mt_rand(170,255),mt_rand(200,255),mt_rand(210,255))); //設置干擾多邊形底圖 /* 文字 */ for ($i = 0; $i < strlen($authnum); $i++){ $font = ImageColorAllocate($im, mt_rand(0,50),mt_rand(0,150),mt_rand(0,200));//设置文字颜色 $x = $i/$length * $pw + rand(1, 6); //设置随机X坐标 $y = rand(1, $ph/3); //设置随机Y坐标 imagestring($im, mt_rand(4,6), $x, $y, substr($authnum,$i,1), $font); } /* 加入干扰象素 */ for($i=0; $i<$pnum; $i++){ $dist = ImageColorAllocate($im, mt_rand(0,255),mt_rand(0,255),mt_rand(0,255)); //设置杂点颜色 imagesetpixel($im, mt_rand(0,$pw) , mt_rand(0,$ph) , $dist); } /* 加入干擾線 */ for($i=0; $i<$lnum; $i++){ $dist = ImageColorAllocate($im, mt_rand(50,255),mt_rand(150,255),mt_rand(200,255)); //設置線顏色 imageline($im,mt_rand(0,$pw),mt_rand(0,$ph),mt_rand(0,$pw),mt_rand(0,$ph),$dist); } ImagePNG($im); //以 PNG 格式将图像输出到浏览器或文件 ImageDestroy($im); //销毁一图像 } /** 檢查驗證碼 * @param String $captcha 驗證碼 * @param int $flag 驗證成功后 0:不清除session 1:清除session * @return boolean */ public function check($captcha,$flag=1){ if(empty($captcha)){ return false; }else{ if(strtoupper($captcha)==$this->captcha_session($this->sname)){ //檢測驗證碼 if($flag==1){ $this->captcha_session($this->sname,''); } return true; }else{ return false; } } } /* 产生随机数函数 * @param int $length 需要隨機生成的字符串數 * @return String */ private function random($length){ $hash = ''; $chars = 'ABCDEFGHIJKLMNPQRSTUVWXYZ23456789'; $max = strlen($chars) - 1; for($i = 0; $i < $length; $i++) { $hash .= $chars[mt_rand(0, $max)]; } return $hash; } /** 驗證碼session處理方法 * @param String $name captcha session name * @param String $value * @return String */ private function captcha_session($name,$value=null){ if(isset($value)){ if($value!==''){ $_SESSION[$name] = $value; }else{ unset($_SESSION[$name]); } }else{ return isset($_SESSION[$name])? $_SESSION[$name] : ''; } } } // class end ?>
demo
<? session_start(); require_once('Captcha.class.php'); $obj = new Captcha($sname); # 創建Captcha類對象 # $sname為保存captcha的session name,可留空,留空則為'm_captcha' $obj->create($length,$param); # 創建Captcha并輸出圖片 # $length為Captcha長度,可留空,默認為4 /* $param = array( 'width' => 13 captcha 字符寬度 'height' => 18 captcha 字符高度 'pnum' => 100 干擾點個數 'lnum' => 2 干擾線條數 ) 可留空 */ $obj->check($captcha,$flag); # 檢查用戶輸入的驗證碼是否正確,true or false # $captcha為用戶輸入的驗證碼,必填 # $flag 可留空,默認為1 # 1:當驗證成功后自動清除captcha session # 0:當驗證成功后不清除captcha session,用於ajax檢查 ?>
本文讲解了关于php Captcha 驗證碼類的相关内容,更多相关知识请关注php中文网。
相关推荐:
以上是关于php Captcha 驗證碼類的讲解的详细内容。更多信息请关注PHP中文网其他相关文章!

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

热门话题

问题发现springboot项目生产session-out超时问题,描述下问题:在测试环境通过改动application.yaml配置session-out,经过设置不同时间验证session-out配置生效,于是就直接设置了过期时间为8小时发布到了生产环境。然而中午接到客户反应项目过期时间设置较短,半小时不操作就会话过期需要反复登陆。解决处理开发环境:springboot项目内置Tomcat,所以项目中application.yaml配置session-out是生效的。生产环境:生产环境发布是

session失效通常是由于 session 的生存时间过期或者服务器关闭导致的。其解决办法:1、延长session的生存时间;2、使用持久化存储;3、使用cookie;4、异步更新session;5、使用会话管理中间件。

PHPSession跨域问题的解决方法在前后端分离的开发中,跨域请求已成为常态。在处理跨域问题时,我们通常会涉及到session的使用和管理。然而,由于浏览器的同源策略限制,跨域情况下默认情况下无法共享session。为了解决这个问题,我们需要采用一些技巧和方法来实现session的跨域共享。一、使用cookie跨域共享session最常

function是函数的意思,是一段具有特定功能的可重复使用的代码块,是程序的基本组成单元之一,可以接受输入参数,执行特定的操作,并返回结果,其目的是封装一段可重复使用的代码,提高代码的可重用性和可维护性。

php session刷新后没有了的解决办法:1、通过“session_start();”开启session;2、把所有的公共配置写在一个php文件内;3、变量名不能和数组下标相同;4、在phpinfo里面查看session数据的存储路径,并查看该文件目录下的sessio是否保存成功即可。

session php默认失效时间是1440秒,也就是24分钟,表示客户端超过24分钟没有刷新,当前session就会失效;如果用户关闭了浏览器,会话就会结束,Session就不存在了。

问题:今天项目中遇到了一个设置时间超时的问题,按SpringBoot2的application.properties更改一直不生效。解决方案:server.*属性用于控制SpringBoot使用的嵌入式容器。SpringBoot将使用ServletWebServerFactory实例之一创建servlet容器的实例。这些类使用server.*属性来配置受控的servlet容器(tomcat,jetty等)。当应用程序作为war文件部署到Tomcat实例时,server.*属性不适用。它们不适用,

1.基于session实现短信登录1.1短信登录流程图1.2实现发送短信验证码前端请求说明:说明请求方式POST请求路径/user/code请求参数phone(电话号码)返回值无后端接口实现:@Slf4j@ServicepublicclassUserServiceImplextendsServiceImplimplementsIUserService{@OverridepublicResultsendCode(Stringphone,HttpSessionsession){//1.校验手机号if
