thinkPHP中驗證碼的簡單實現方法

來源:互聯網
上載者:User
這篇文章主要介紹了thinkPHP中驗證碼的簡單實現方法,結合執行個體形式分析了thinkPHP驗證碼的實現原理、步驟與調用技巧,需要的朋友可以參考下

本文執行個體講述了thinkPHP中驗證碼的簡單實現方法。分享給大家供大家參考,具體如下:

運行如下:

1.php端產生驗證碼函數

public function verify(){    // 驗證碼    import("@.Util.Image");    Image::buildImageVerify(4,1,'png',40,20,'verify');}/*** 產生映像驗證碼* @static* @access public* @param string $length 位元* @param string $mode 類型* @param string $type 映像格式* @param string $width 寬度* @param string $height 高度* @return string*/static function buildImageVerify($length=4, $mode=1, $type='png', $width=48, $height=22, $verifyName='verify') {    import('ORG.Util.String');    $randval = String::randString($length, $mode);    session($verifyName, md5($randval));    $width = ($length * 10 + 10) > $width ? $length * 10 + 10 : $width;    if ($type != 'gif' && function_exists('imagecreatetruecolor')) {      $im = imagecreatetruecolor($width, $height);    } else {      $im = imagecreate($width, $height);    }    $r = Array(225, 255, 255, 223);    $g = Array(225, 236, 237, 255);    $b = Array(225, 236, 166, 125);    $key = mt_rand(0, 3);    $backColor = imagecolorallocate($im, $r[$key], $g[$key], $b[$key]);  //背景色(隨機)    $borderColor = imagecolorallocate($im, 100, 100, 100); //邊框色    imagefilledrectangle($im, 0, 0, $width - 1, $height - 1, $backColor);    imagerectangle($im, 0, 0, $width - 1, $height - 1, $borderColor);    $stringColor = imagecolorallocate($im, mt_rand(0, 200), mt_rand(0, 120), mt_rand(0, 120));    // 幹擾    for ($i = 0; $i < 10; $i++) {      imagearc($im, mt_rand(-10, $width), mt_rand(-10, $height), mt_rand(30, 300), mt_rand(20, 200), 55, 44, $stringColor);    }    for ($i = 0; $i < 25; $i++) {      imagesetpixel($im, mt_rand(0, $width), mt_rand(0, $height), $stringColor);    }    for ($i = 0; $i < $length; $i++) {      imagestring($im, 5, $i * 10 + 5, mt_rand(1, 8), $randval{$i}, $stringColor);    }    Image::output($im, $type);}

產生圖片的同時,傳入到session中。

2.頁面端

<img id="verifyImg" src="{sh::U('Agent/Login/verify')}" onClick="changeVerify()" title="點擊重新整理驗證碼" /></p>

直接src中調用。

點擊觸發改變。

function changeVerify(){ verifyURL = "{sh::U('Agent/Login/verify')}"; $("#verifyImg").attr("src",verifyURL); return false;}

3.後台驗證,對比post欄位與session中的驗證碼是否一致。

if($_SESSION['verify'] != md5($_POST['verify'])) {  $this->error('驗證碼錯誤!');}

以上就是本文的全部內容,希望對大家的學習有所協助,更多相關內容請關注topic.alibabacloud.com!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.