佳木斯湛栽影视文化发展公司

主頁 > 知識庫 > PHP生成加減算法方式的驗證碼實例

PHP生成加減算法方式的驗證碼實例

熱門標簽:電子圍欄 科大訊飛語音識別系統(tǒng) 銀行業(yè)務(wù) 服務(wù)器配置 團購網(wǎng)站 Mysql連接數(shù)設(shè)置 Linux服務(wù)器 阿里云

這是在網(wǎng)上找的一個demo,我加入了一部分代碼??梢允褂?。

這里需要說明一下,我們調(diào)用這個驗證碼類應(yīng)該在一個單獨的控制器方法中使用。

生成的圖片的算法是用代碼生成的,然后把計算值存入session中。

驗證的時候是獲取用戶的輸入值,然后取出服務(wù)端的值進行對比

?php 
namespace mobile\components;
/** 
 * @author fenghuo 
 * 
 * 改造的加減法驗證類 
 * 使用示例 VerifyCode::get(1,2); 
 * 驗證示例 VerifyCode::check($code); 
 */ 
class VerifyCode 
{ 
  /** 
   * php驗證碼 
   */ 
  public static function get($one,$two,$prefix = '', $font_size = 28) 
  { 
    //文件頭... 
    ob_get_clean();
    header("Content-type: image/png;charset=utf-8;"); 
    //創(chuàng)建真彩色白紙 
    $width      = $font_size*5; 
    $height      = $font_size+1; 
    $im        = @imagecreatetruecolor($width, $height) or die("建立圖像失敗"); 
    //獲取背景顏色 
    $background_color = imagecolorallocate($im, 255, 255, 255); 
    //填充背景顏色 
    imagefill($im, 0, 0, $background_color); 
    //獲取邊框顏色 
    $border_color   = imagecolorallocate($im, 200, 200, 200); 
    //畫矩形,邊框顏色200,200,200 
    imagerectangle($im,0,0,$width - 1, $height - 1,$border_color); 
    //逐行炫耀背景,全屏用1或0 
    for($i = 2;$i  $height - 2;$i++) { 
      //獲取隨機淡色 
      $line_color = imagecolorallocate($im, rand(200,255), rand(200,255), rand(200,255)); 
      //畫線 
      imageline($im, 2, $i, $width - 1, $i, $line_color); 
    } 
    //設(shè)置印上去的文字 
    $firstNum = $one; 
    $secondNum = $two; 
    $actionStr = $firstNum > $secondNum ? '-' : '+'; 
    //獲取第1個隨機文字 
    $imstr[0]["s"] = $firstNum; 
    $imstr[0]["x"] = rand(2, 5); 
    $imstr[0]["y"] = rand(1, 4); 
    //獲取第2個隨機文字 
    $imstr[1]["s"] = $actionStr; 
    $imstr[1]["x"] = $imstr[0]["x"] + $font_size - 1 + rand(0, 1); 
    $imstr[1]["y"] = rand(1,5); 
    //獲取第3個隨機文字 
    $imstr[2]["s"] = $secondNum; 
    $imstr[2]["x"] = $imstr[1]["x"] + $font_size - 1 + rand(0, 1); 
    $imstr[2]["y"] = rand(1, 5); 
    //獲取第3個隨機文字 
    $imstr[3]["s"] = '='; 
    $imstr[3]["x"] = $imstr[2]["x"] + $font_size - 1 + rand(0, 1); 
    $imstr[3]["y"] = 3; 
    //獲取第3個隨機文字 
    $imstr[4]["s"] = '?'; 
    $imstr[4]["x"] = $imstr[3]["x"] + $font_size - 1 + rand(0, 1); 
    $imstr[4]["y"] = 3; 
    //文字 
    $text = ''; 
    //寫入隨機字串 
    for($i = 0; $i  5; $i++) { 
      //獲取隨機較深顏色 
      $text_color = imagecolorallocate($im, rand(50, 180), rand(50, 180), rand(50, 180)); 
      $text .= $imstr[$i]["s"]; 
      //畫文字 
      imagechar($im, $font_size, $imstr[$i]["x"], $imstr[$i]["y"], $imstr[$i]["s"], $text_color); 
    } 
    session_start();
    $_SESSION[$prefix.'verifycode'] = $firstNum > $secondNum ? ($firstNum - $secondNum) : ($firstNum + $secondNum); 
    //顯示圖片 
    ImagePng($im); 
    //銷毀圖片 
    ImageDestroy($im); 
  } 
  public static function check($code) 
  { 
    if(trim($_SESSION[$prefix.'verifycode']) == trim($code)) { 
      return true; 
    } else { 
      return false; 
    } 
  } 
} 

以上這篇PHP生成加減算法方式的驗證碼實例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。

您可能感興趣的文章:
  • php實現(xiàn)加減法驗證碼代碼
  • php 生成唯一id的幾種解決方法

標簽:棗莊 大理 蚌埠 廣元 江蘇 萍鄉(xiāng) 衢州 衡水

巨人網(wǎng)絡(luò)通訊聲明:本文標題《PHP生成加減算法方式的驗證碼實例》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266
    咸宁市| 容城县| 蚌埠市| 纳雍县| 林周县| 安龙县| 淮南市| 泸水县| 高州市| 弋阳县| 荥阳市| 定南县| 平凉市| 旌德县| 来宾市| 通州市| 怀来县| 景德镇市| 东兴市| 武安市| 涞水县| 砚山县| 揭东县| 东乌| 梁平县| 施秉县| 拉萨市| 周至县| 嘉黎县| 项城市| 津南区| 政和县| 织金县| 资源县| 竹北市| 武汉市| 同江市| 沅江市| 大厂| 乌兰县| 乌拉特后旗|