PHP產生加減演算法步驟詳解

來源:互聯網
上載者:User
這次給大家帶來PHP產生加減演算法步驟詳解,PHP產生加減演算法的注意事項有哪些,下面就是實戰案例,一起來看一下。

這裡需要說明一下,我們調用這個驗證碼類應該在一個單獨的控制器方法中使用。

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

驗證的時候是擷取使用者的輸入值,然後取出服務端的值進行對比

<?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;");     //建立真彩色白紙     $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);     }     //設定印上去的文字     $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中文網其它相關文章!

推薦閱讀:

php刪除二維數組內重複值方法總結

實現php合并數組並保留索引值有哪些方法?

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.