中文字幕在线观看,亚洲а∨天堂久久精品9966,亚洲成a人片在线观看你懂的,亚洲av成人片无码网站,亚洲国产精品无码久久久五月天

簡(jiǎn)單好用的PHP驗(yàn)證碼類

2018-07-20    來源:open-open

容器云強(qiáng)勢(shì)上線!快速搭建集群,上萬Linux鏡像隨意使用
/**
* @PHP驗(yàn)證碼類
* 使用方法:
* $image=new Captcha();
* $image->config('寬度','高度','字符個(gè)數(shù)','驗(yàn)證碼session索引');
* $image->create();//這樣就會(huì)向?yàn)g覽器輸出一張圖片
//http://blog.ddian.cn
* //所有參數(shù)都可以省略,
* 默認(rèn)是:寬80 高20 字符數(shù)4 驗(yàn)證碼session索引captcha_code
* 第四個(gè)參數(shù)即把驗(yàn)證碼存到$_SESSION['captcha_code']
* 最簡(jiǎn)單使用示例:
* $image=new Captcha();
* $image->create();//這樣就會(huì)向?yàn)g覽器輸出一張圖片
*/
class Captcha
{
private $width=80,$height=20,$codenum=4;
public $checkcode; //產(chǎn)生的驗(yàn)證碼
private $checkimage; //驗(yàn)證碼圖片
private $disturbColor = ''; //干擾像素
private $session_flag='captcha_code';//存到session中的索引
//嘗試開始session
function __construct(){
@session_start();
}
/*
* 參數(shù):(寬度,高度,字符個(gè)數(shù))
*/
function config($width='80',$height='20',$codenum='4',$session_flag='captcha_code')
{
$this->width=$width;
$this->height=$height;
$this->codenum=$codenum;
$this->session_flag=$session_flag;
}
function create()
{
//輸出頭
$this->outFileHeader();
//產(chǎn)生驗(yàn)證碼
$this->createCode();
//產(chǎn)生圖片
$this->createImage();
//設(shè)置干擾像素
$this->setDisturbColor();
//往圖片上寫驗(yàn)證碼
$this->writeCheckCodeToImage();
imagepng($this->checkimage);
imagedestroy($this->checkimage);
$_SESSION[$this->session_flag]=$this->checkcode;
}
/*
* @brief 輸出頭
*/
private function outFileHeader()
{
header ("Content-type: image/png");
}
/**
* 產(chǎn)生驗(yàn)證碼
*/
private function createCode()
{
$this->checkcode = strtoupper(substr(md5(rand()),0,$this->codenum));
}
/**
* 產(chǎn)生驗(yàn)證碼圖片
*/
private function createImage()
{
$this->checkimage = @imagecreate($this->width,$this->height);
$back = imagecolorallocate($this->checkimage,255,255,255);
$border = imagecolorallocate($this->checkimage,0,0,0);
imagefilledrectangle($this->checkimage,0,0,$this->width - 1,$this->height - 1,$back); // 白色底
imagerectangle($this->checkimage,0,0,$this->width - 1,$this->height - 1,$border); // 黑色邊框
}
/**
* 設(shè)置圖片的干擾像素
*/
private function setDisturbColor()
{
for ($i=0;$i<=200;$i++)
{
$this->disturbColor = imagecolorallocate($this->checkimage, rand(0,255), rand(0,255), rand(0,255));
imagesetpixel($this->checkimage,rand(2,128),rand(2,38),$this->disturbColor);
}
}
/**
*
* 在驗(yàn)證碼圖片上逐個(gè)畫上驗(yàn)證碼
*
*/
private function writeCheckCodeToImage()
{
for ($i=0;$i<$this->codenum;$i++)
{
$bg_color = imagecolorallocate ($this->checkimage, rand(0,255), rand(0,128), rand(0,255));
$x = floor($this->width/$this->codenum)*$i;
$y = rand(0,$this->height-15);
imagechar ($this->checkimage, rand(5,8), $x+5, $y, $this->checkcode[$i], $bg_color);
}
}
function __destruct()
{
unset($this->width,$this->height,$this->codenum,$this->session_flag);
}
}
?>
 

標(biāo)簽:

版權(quán)申明:本站文章部分自網(wǎng)絡(luò),如有侵權(quán),請(qǐng)聯(lián)系:west999com@outlook.com
特別注意:本站所有轉(zhuǎn)載文章言論不代表本站觀點(diǎn)!
本站所提供的圖片等素材,版權(quán)歸原作者所有,如需使用,請(qǐng)與原作者聯(lián)系。

上一篇:Android判斷網(wǎng)絡(luò)狀態(tài)方法

下一篇:iOS晃動(dòng)檢測(cè)