<?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??參數(shù) *?@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; //干擾象素個數(shù) $lnum =?isset($param['lnum'])??$param['lnum']?:?2; //干擾線條數(shù) $this->captcha_session($this->sname,$authnum); //將隨機數(shù)寫入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; } } } /*?產(chǎn)生隨機數(shù)函數(shù) *?@param int $length 需要隨機生成的字符串數(shù) *?@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); # 創(chuàng)建Captcha類對象 # $sname為保存captcha的session name,可留空,留空則為'm_captcha' $obj->create($length,$param); #?創(chuàng)建Captcha并輸出圖片 #?$length為Captcha長度,可留空,默認為4 /*?$param?=?array( 'width'?=>?13 captcha?字符寬度 'height'?=>?18 captcha?字符高度 'pnum'?=>?100 干擾點個數(shù) 'lnum'?=>?2 干擾線條數(shù) ) 可留空 */ $obj->check($captcha,$flag); #?檢查用戶輸入的驗證碼是否正確,true?or?false #?$captcha為用戶輸入的驗證碼,必填 #?$flag?可留空,默認為1? # 1:當驗證成功后自動清除captcha?session # 0:當驗證成功后不清除captcha?session,用於ajax檢查 ?>
本文講解了關于php Captcha 驗證碼類的相關內(nèi)容,更多相關知識請關注php中文網(wǎng)。
相關推薦:
MySQL的information_schema 相關內(nèi)容
查看mysql數(shù)據(jù)庫大小、表大小和最后修改時間
以上是關于php Captcha 驗證碼類的講解的詳細內(nèi)容。更多資訊請關注PHP中文網(wǎng)其他相關文章!

熱AI工具

Undress AI Tool
免費脫衣圖片

Undresser.AI Undress
人工智慧驅動的應用程序,用於創(chuàng)建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

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

禪工作室 13.0.1
強大的PHP整合開發(fā)環(huán)境

Dreamweaver CS6
視覺化網(wǎng)頁開發(fā)工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

問題發(fā)現(xiàn)springboot專案生產(chǎn)session-out逾時問題,描述下問題:在測試環(huán)境透過改動application.yaml配置session-out,經(jīng)過設定不同時間驗證session-out配置生效,於是就直接設定了過期時間為8小時發(fā)布到了生產(chǎn)環(huán)境。然而中午接到客戶反應項目過期時間設定較短,半小時不操作就會話過期需要重複登陸。解決處理開發(fā)環(huán)境:springboot專案內(nèi)建Tomcat,所以專案中application.yaml配置session-out是生效的。生產(chǎn)環(huán)境:生產(chǎn)環(huán)境發(fā)布是

php session刷新後沒有了的解決方法:1、透過「session_start();」開啟session;2、把所有的公共配置寫在一個php檔案內(nèi);3、變數(shù)名稱不能和陣列下標相同;4、在phpinfo裡面查看session資料的儲存路徑,並查看該檔案目錄下的sessio是否儲存成功即可。

session失效通常是由於 session 的生存時間過期或伺服器關閉導致的。其解決方法:1、延長session的生存時間;2、使用持久化儲存;3、使用cookie;4、非同步更新session;5、使用會話管理中介軟體。

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

問題:今天專案中遇到了一個設定時間逾時的問題,按SpringBoot2的application.properties變更一直不生效。解決方案:server.*屬性用於控制SpringBoot所使用的嵌入式容器。 SpringBoot將使用ServletWebServerFactory實例之一來建立servlet容器的執(zhí)行個體。這些類別使用server.*屬性來配置受控的servlet容器(tomcat,jetty等)。當應用程式作為war檔部署到Tomcat實例時,server.*屬性不適用。它們不適用,

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

session php預設失效時間是1440秒,也就是24分鐘,表示客戶端超過24分鐘沒有刷新,當前session就會失效;如果使用者關閉了瀏覽器,會話就會結束,Session就不存在了。

1.基於session實作簡訊登入1.1簡訊登入流程圖1.2實作發(fā)送簡訊驗證碼前端請求說明:說明請求方式POST請求路徑/user/code請求參數(shù)phone(電話號碼)回傳值無後端介面實作:@Slf4j@ ServicepublicclassUserServiceImplextendsServiceImplimplementsIUserService{@OverridepublicResultsendCode(Stringphone,HttpSessionsession){//1.校驗手機號碼if
