分享一個php 驗證碼類別及呼叫範例
本文分享一个不错的php 验证码类,后面附有完整的调用示例,有需要的朋友,参考下吧。
1,php验证码类 <?php // usage: /* 显示验证码: <img src="/static/imghw/default1.png" data-src="captcha.php?cap=login.png" class="lazy" alt="分享一個php 驗證碼類別及呼叫範例" > 检查验证码: 检查输入的验证码与 $_SESSION['login'] 中保存的值是否相等。 */ error_reporting(E_ALL); session_start(); (!isset($_GET['cap']))?die('Error !'):1; $captcha_array=array('login.png','contact.png','comment.png'); (!in_array($_GET['cap'],$captcha_array))?die('Error !'):1; $captcha_cod=new captcha(basename($_GET['cap'],'.png')) ; //验证码类 class captcha { private $session_name; private $image_width; private $image_height; private $cod_length; private $cod_mode; private $font_path; private $avtage_font_size; private $sec_cod; private $res_image; function __construct($name,$width=200,$height=50,$length=5,$mod=2,$font='arial.ttf',$av_font_size=25) { $this-> session_name = $name ; $this-> image_width = $width ; $this-> image_height = $height ; $this-> cod_length = $length ; $this-> mode = $mod ; $this-> font_path = $font ; $this-> avrage_font_size = $av_font_size ; $this->Gen_Cod(); } function Write_Text($text) { $x_pos=10; for($pos=0;$pos<strlen($text);$pos++) { imagettftext($this->res_image,rand($this->avrage_font_size -2,$this->avrage_font_size +2), rand(-40,+40),$x_pos,rand(35,$this->image_height - $this->avrage_font_size), imagecolorallocate($this->res_image,rand(0,150),rand(0,150),rand(0,150)), $this->font_path,$text[$pos]); $x_pos+=($this->image_width/$this->cod_length); } } function Draw_Line() { // for($pos=0;$pos<$this->image_height;$pos+=8) imageline($this->res_image,0,$pos,$this->image_width,$pos,imagecolorallocate($this->res_image,rand(200,230),rand(200,230),rand(200,230))); // for($pos=0;$pos<$this->image_width;$pos+=8) imageline($this->res_image,$pos,0,$pos,$this->image_height,imagecolorallocate($this->res_image,rand(200,230),rand(200,230),rand(200,230))); } function Gen_Cod() { //generate rand cod : //mode:1 => 0-9 , mode:2 => 0-9 , a-z ($this->mode==1) ? $this->sec_cod=substr((string)rand(1000000000,9999999999),0,$this->cod_length) : $this->sec_cod=substr(md5(rand(1000000000,9999999999)),0,$this->cod_length); //set session : $_SESSION[$this->session_name] = $this->sec_cod ; //creat image : $this->res_image=imagecreatetruecolor( $this->image_width , $this->image_height ); //fill color: imagefilledrectangle($this->res_image,0,0,$this->image_width,$this->image_height,imagecolorallocate($this->res_image,255,255,255)); //write text : $this->Write_Text($this->sec_cod); //draw line : $this->Draw_Line(); //output : imagejpeg($this->res_image); header('content-type:image/jpeg'); //destroy: imagedestroy($this->res_image); } } 登入後複製 2,php验证码类的调用示例: <?php session_start(); if(isset($_POST['captchacod'])){ if($_SESSION['login']==$_POST['captchacod'])echo'Your Entered Cod Was Correct<br>'; else echo'Your Entered Cod Was Incorrect<br>'; } ?> <img src="/static/imghw/default1.png" data-src="captcha.php?cap=login.png" class="lazy" alt="分享一個php 驗證碼類別及呼叫範例" > <form action="<?php echo $_SERVER['PHP_SELF']; //safe it later (xss)?>" method="post"> INPUT TEXT :<br> <input type="text" name="captchacod"><br> <input type="submit" value="check"><br> </form> 登入後複製 |
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

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

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

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章
R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
3 週前
By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳圖形設置
3 週前
By 尊渡假赌尊渡假赌尊渡假赌
刺客信條陰影:貝殼謎語解決方案
2 週前
By DDD
R.E.P.O.如果您聽不到任何人,如何修復音頻
3 週前
By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25:如何解鎖Myrise中的所有內容
4 週前
By 尊渡假赌尊渡假赌尊渡假赌

熱工具

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

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

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

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

熱門話題

PHP客戶端URL(curl)擴展是開發人員的強大工具,可以與遠程服務器和REST API無縫交互。通過利用Libcurl(備受尊敬的多協議文件傳輸庫),PHP curl促進了有效的執行

文章討論了PHP 5.3中介紹的PHP中的晚期靜態結合(LSB),允許靜態方法的運行時間分辨率調用以更靈活的繼承。 LSB的實用應用和潛在的觸摸

JWT是一種基於JSON的開放標準,用於在各方之間安全地傳輸信息,主要用於身份驗證和信息交換。 1.JWT由Header、Payload和Signature三部分組成。 2.JWT的工作原理包括生成JWT、驗證JWT和解析Payload三個步驟。 3.在PHP中使用JWT進行身份驗證時,可以生成和驗證JWT,並在高級用法中包含用戶角色和權限信息。 4.常見錯誤包括簽名驗證失敗、令牌過期和Payload過大,調試技巧包括使用調試工具和日誌記錄。 5.性能優化和最佳實踐包括使用合適的簽名算法、合理設置有效期、

使用PHP的cURL庫發送JSON數據在PHP開發中,經常需要與外部API進行交互,其中一種常見的方式是使用cURL庫發送POST�...

深入解讀ReactPHP的非阻塞特性ReactPHP的一段官方介紹引起了不少開發者的疑問:“ReactPHPisnon-blockingbydefault....
