CodeIgniter tips:驗證碼幫助類

在CI中,作驗證類能夠這樣作,首先給出的是手冊中的作法

加載輔助函數
用下面的代碼加載驗證碼輔助函數: 網絡營銷培訓

$this->load->helper('captcha');

可用的函數以下:

captcha_create($data)
根據你指定的一系列參數建立驗證碼圖像, 返回值是一個包含此圖像數據的數組.

[array]
(
  'image' => IMAGE TAG
  'time' => TIMESTAMP (毫秒)
  'word' => CAPTCHA WORD
)

"image"是實際存在image標記:
<img src="http://example.com/captcha/12345.jpg" width="140" height="50" />

這裏的"time"是一個毫秒級的時間戳,做爲圖片文件名(不包含擴展名). 就像這樣: 1139612155.3422

"word"是驗證碼, 若是不提供, 將是一個隨機字符串.

使用驗證碼輔助函數:
加載後你能夠向這樣產生一個驗證碼:

$vals = array(
    'word' => 'Random word',
    'img_path' => './captcha/',
    'img_url' => 'http://example.com/captcha/',
    'font_path' => './path/to/fonts/texb.ttf',
    'img_width' => '150',
    'img_height' => 30,
    'expiration' => 7200
    );

$cap = create_captcha($vals);
echo $cap['image'];

驗證碼輔助函數必須須要GD庫.
只有 img_path 和 img_url 參數是必須的.
若是"word"未提供, 將自動產生一個ASCII字符串. 你也能夠使用本身的詞庫,從裏面隨機挑選.
若是未提供TRUE TYPE字體的路徑, 將會使用GD自帶的字體.
"captcha" 目錄必須可寫(666, or 777)
"expiration" (秒) 指定了驗證碼圖片的超時刪除時間. 默認是2小時.
配合數據庫
爲了在提交表單時用到驗證,你須要將create_captcha()生成的結果保存到數據庫。這樣,當用戶提交表單時,你就能夠驗證數據庫裏是否有此驗證碼或是否過時。

這是一個數據表的例子:

CREATE TABLE captcha (
captcha_id bigint(13) unsigned NOT NULL auto_increment,
captcha_time int(10) unsigned NOT NULL,
ip_address varchar(16) default '0' NOT NULL,
word varchar(20) NOT NULL,
PRIMARY KEY `captcha_id` (`captcha_id`),
KEY `word` (`word`)
);

這是一個使用數據庫的例子. 一個帶驗證碼的頁面顯示以下:

$this->load->helper('captcha');
$vals = array(
    'img_path' => './captcha/',
    'img_url' => 'http://example.com/captcha/'
    );

$cap = create_captcha($vals);

$data = array(
    'captcha_time' => $cap['time'],
    'ip_address' => $this->input->ip_address(),
    'word' => $cap['word']
    );

$query = $this->db->insert_string('captcha', $data);
$this->db->query($query);

echo '提交下面的驗證碼:';
echo $cap['image'];
echo '<input type="text" name="captcha" value="" />';

而後頁面提交後以下處理:

// 首先刪除舊的驗證碼
$expiration = time()-7200; // 2小時限制
$this->db->query("DELETE FROM captcha WHERE captcha_time < ".$expiration);

// 而後再看是否有驗證碼存在:
$sql = "SELECT COUNT(*) AS count FROM captcha WHERE word = ? AND ip_address = ? AND date > ?";
$binds = array($_POST['captcha'], $this->input->ip_address(), $expiration);
$query = $this->db->query($sql, $binds);
$row = $query->row();

if ($row->count == 0)
{
    echo "你必須提交圖像上顯示的驗證碼";
}


第一種作法時,提交時,也驗證驗證碼是否過時,比較嚴格, 而後是第2種作法,本身簡單定義一個,好比captha.php,代碼以下:
   ?php
session_start();
//登陸驗證碼
function GetVerify($length)
{
$strings = Array('3','4','5','6','7','a','b','c','d','e','f','h','i','j','k','m','n','p','r','s','t','u','v','w','x','y');
$chrNum = "";
$count = count($strings);
for ($i = 1; $i <= $length; $i++) { //循環隨機取字符生成字符串
$chrNum .= $strings[rand(0,$count-1)];
}
return $chrNum;
}
function code(){
$fontSize = 20; //定義字體大小
$length = 5; //定義字符串長度
$strNum = GetVerify($length); //獲取一個隨機字符串
$_SESSION['verify'] = $strNum; //付值給session
$width = 90; //定義圖片寬度
$height = 30; //定義圖片高度
$im = imagecreate($width,$height); //生成一張指定寬高的圖片
$backgroundcolor = imagecolorallocate ($im, 255, 255, 255); //生成背景色
$frameColor = imageColorAllocate($im, 150, 150, 150); //生成邊框色
$font = './system/fonts/arial.ttf'; //提取字體文件,開始寫字
for($i = 0; $i < $length; $i++) {
$charY = ($height+9)/2 + rand(-1,1); //定義字符Y座標
$charX = $i*15+8; //定義字符X座標
//生成字符顏色
$text_color = imagecolorallocate($im, mt_rand(50, 200), mt_rand(50, 128), mt_rand(50, 200));
$angle = rand(-20,20); //生成字符角度
//寫入字符
imageTTFText($im, $fontSize, $angle, $charX,  $charY, $text_color, $font, $strNum[$i]);
}
for($i=0; $i <= 5; $i++) { //循環畫背景線
$linecolor = imagecolorallocate($im, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
$linex = mt_rand(1, $width-1);
$liney = mt_rand(1, $height-1);
imageline($im, $linex, $liney, $linex + mt_rand(0, 4) - 2, $liney + mt_rand(0, 4) - 2, $linecolor);
}
for($i=0; $i <= 32; $i++) { //循環畫背景點,生成麻點效果
$pointcolor = imagecolorallocate($im, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
imagesetpixel($im, mt_rand(1, $width-1), mt_rand(1, $height-1), $pointcolor);
}
imagerectangle($im, 0, 0, $width-1 , $height-1 , $frameColor); //畫邊框
//ob_clean();
//header('Content-type: image/png');
imagepng($im);
imagedestroy($im);
}
?>
  而後把這個文件放在helper下,而後調用時能夠這樣:
  $this->load->helper('captcha');
   code(); / 網絡營銷培訓(fblww-0209)
相關文章
相關標籤/搜索