《PHP實戰(zhàn):php驗證碼生成器》要點:
本文介紹了PHP實戰(zhàn):php驗證碼生成器,希望對您有用。如果有疑問,可以聯(lián)系我們。
PHP學(xué)習(xí)現(xiàn)在很多網(wǎng)站都有實現(xiàn)用戶集.然而為了防止機器人的網(wǎng)絡(luò)攻擊.限制登陸或者注冊是有必要的.
在注冊和登陸時強制要求輸入一個機器難以識別的字符串集是一個不錯的選擇.雖然不能解決根本問題,但至少可以增加他們的成本.
PHP學(xué)習(xí)利用PHP生成驗證碼需要用到GD2庫.GD2庫引用方法網(wǎng)絡(luò)上有很多,不同操作系統(tǒng)導(dǎo)入方式也不同.
PHP學(xué)習(xí)這段代碼運行在WINDOS服務(wù)器平臺
PHP學(xué)習(xí)
<?php
$iC = new idCode(5,60,30);
$iC->createPNG();
class idCode{
private $words = array('a','b',
'c','d','e','f','g','h','i','j','k','l',
'm','n','o','p','q','r','s','t','u','v',
'w','x','y','z','A','B','C','D','E','F',
'G','H','I','J','K','L','M','N','O','P',
'Q','R','S','T','U','V','W','X','Y','Z',
'0','1','2','3','4','5','6','7','8','9');
private $fonts;
private $count;//驗證碼字符數(shù)
private $height;
private $width;
private $path = '..\myfolder\fonts';
private $keys;
//構(gòu)造函數(shù)
public function __construct($count,$width,$height){
$this->count = $count;
$this->getFonts();
$this->height = $height;
$this->width = $width;
}
private function getFonts(){
$dir = dir($this->path);
while(false !== ($file = $dir->read())){
if($file != '.' && $file != '..'){
$this->fonts[count($this->fonts)] = basename($file);
}
}
$dir->close();
}
private function createKeys(){
for($i = 0;$i < $this->count;$i++){
$this->keys[$i]['char'] = $this->words[rand(0,count($this->words)-1)];
//使用字體路徑標(biāo)識
$this->keys[$i]['filename'] = $this->path.'\\'.$this->fonts[rand(0,count($this->fonts)-1)];
}
}
public function createPNG(){
$this->createKeys();
//創(chuàng)建畫布以及顏色塊兒
$bg = imagecreatetruecolor($this->width + 10*2,$this->height + 3*2);//兩邊留10px空白,上下3px
$grey = imagecolorallocate($bg,155,155,155);
$blue = imagecolorallocate($bg,0x00,0x00,0xff);
//填充背景
imagefill($bg,0,0,$grey);
//添加字符
$pwidth = $this->width/$this->count;
$x;$y;
for($i = 0;$i < $this->count;$i++){
$rotation = rand(-40,40);//偏轉(zhuǎn)角度±40°
$fontsize = 33;
$width_txt;
$height_txt;
do{
$fontsize--;
$bbox = imagettfbbox($fontsize,$rotation,$this->keys[$i]['filename'],$this->keys[$i]['char']);
$width_txt = $bbox[2] - $bbox[0];//x 0 2 4 6,y1 3 5 7;左下,右下,右上,左上
$height_txt = $bbox[7] - $bbox[1];
}while($fontsize > 8 && ($height_txt > $this->height || $width_txt > $pwidth));
$fontcolor = imagecolorallocate($bg,rand(0,255),rand(0,255),rand(0,255));
$x = 8 + $pwidth*$i + $pwidth/2 - $width_txt/2;//x坐標(biāo)基本位置
$y = $this->height/2 - $height_txt/2;
imagettftext($bg,$fontsize,$rotation,$x,$y,$fontcolor,$this->keys[$i]['filename'],$this->keys[$i]['char']);
}
//繪制干擾線
//根據(jù)字體酌情增加干擾線
imageline($bg,0,15,40,10,$blue);
//圖像輸出頭文件
header('Content-type:image/png');
//輸出png圖像
imagepng($bg);
//清除緩存資源
imagedestroy($bg);
}
public function checkKeys($input){
if(count($input)!=$this->count){
return 'ERROR:長度不正確.';
}else{
for($i=0;$i < $this->count;$i++){
//0 o O I l 1 校準(zhǔn),根據(jù)所選擇的字體確定是否需要手動校準(zhǔn)
if($input[$i] != $this->keys[$i]['char']){
return 'SUCCESS.';
}else{
return 'ERROR:請輸入正確驗證碼.';
}
}
}
}
}
?>
PHP學(xué)習(xí)以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持維易PHP.
轉(zhuǎn)載請注明本頁網(wǎng)址:
http://www.snjht.com/jiaocheng/739.html