关于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、使用會話管理中介軟體。

function是函數的意思,是一段具有特定功能的可重複使用的程式碼區塊,是程式的基本組成單元之一,可以接受輸入參數,執行特定的操作,並傳回結果,其目的是封裝一段可重複使用的程式碼,提高程式碼的可重複使用性和可維護性。

PHPSession跨域問題的解決方法在前後端分離的開發中,跨域請求已成為常態。在處理跨域問題時,我們通常會涉及session的使用和管理。然而,由於瀏覽器的同源策略限制,跨域情況下預設無法共享session。為了解決這個問題,我們需要採用一些技巧和方法來實現session的跨域共享。一、使用cookie跨域共享session最常

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
