溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

使用php怎么編寫一個加減法驗(yàn)證碼功能

發(fā)布時間:2021-01-16 11:24:17 來源:億速云 閱讀:209 作者:Leah 欄目:開發(fā)技術(shù)

這篇文章給大家介紹使用php怎么編寫一個加減法驗(yàn)證碼功能,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。

 代碼如下:

<?php
/*圖片驗(yàn)證碼文件,加減計(jì)算方式*/

class ImageCode{

 private $Jiashu  = 0;        //加數(shù)或者減數(shù)
 private $JianShu = 0;        //被加數(shù)或者被減數(shù)
 private $YunSuan = '';       //運(yùn)算符
 private $DeShu   = 0;        //得數(shù)
 private $String  = '';       //字符串樣式
 private $Img;                //圖片對象
 private $Width   = 100;      //圖片寬度
 private $Height  = 50;       //圖片高度
 private $Ttf     = 'Num.ttf';//字體文件
 private $Session = 'code';   //Session變量

 private function JiaShu(){
  header('Content-type:image/png');
  $this -> Jiashu  = rand(1, 10);
  $this -> JianShu = rand(1, 10);
  $this -> YunSuan= $this -> Jiashu > $this -> JianShu ? '-' : '+';
  $this -> DeShu   = $this -> Jiashu > $this -> JianShu ? $this -> Jiashu - $this -> JianShu : $this -> Jiashu + $this -> JianShu;
 }

 public function Show( $W = 100, $H = 50, $T = 'Num.ttf', $Code = 'code' ){
  $this -> JiaShu();
  $this -> String = $this -> Jiashu . $this -> YunSuan . $this -> JianShu . '= ? ';
  $this -> Width  = $W;
  $this -> Height = $H;
  $this -> Ttf    = $T;
  $this -> Session= $Code;
  session_start();
  $_SESSION[$this -> Session] = $this -> DeShu;
  $this -> Images();
 }

 private function Images(){
  $this -> Img = imagecreate($this -> Width, $this -> Height);
  $background_color = imagecolorallocate ($this -> Img, 255, 255, 255);
  imagecolortransparent($this -> Img, $background_color);
        imagettftext($this -> Img, 14, 0, 1, 20, imagecolorallocate ($this -> Img, 0, 0, 0), $this -> Ttf, $this -> String );
  $this -> EchoImages();
 }

 private function EchoImages(){
  imagepng($this -> Img);
  imagedestroy($this -> Img);
 }

}

$ImageCode = new ImageCode;
$ImageCode -> Show(130, 35, 'Num.ttf', 'code');

關(guān)于使用php怎么編寫一個加減法驗(yàn)證碼功能就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

php
AI