一個漂亮的php驗證碼類
編程的魅力在于可以實現(xiàn)想要的功能,下面小編就為大家分享一個漂亮的php驗證碼類。需要的朋友可以過來參考下,更多消息請關注應屆畢業(yè)生網(wǎng)。
直接上代碼,代碼如下:
/pic/p>
class ValidateCode {
private $charset = 'abcdefghkmnprstuvwxyzABCDEFGHKMNPRSTUVWXYZ23456789';/pic/p>
private $code;/pic/p>
private $codelen = 4;/pic/p>
private $width = 130;/pic/p>
private $height = 50;/pic/p>
private $img;/pic/p>
private $font;/pic/p>
private $fontsize = 20;/pic/p>
private $fontcolor;/pic/p>
/pic/p>
public function __construct() {
$this->font = dirname(__FILE__).'/font/elephant.ttf';/pic/p>
}
/pic/p>
private function createCode() {
$_len = strlen($this->charset)-1;
for ($i=0;$i<$this->codelen;$i++) {
$this->code .= $this->charset[mt_rand(0,$_len)];
}
}
/pic/p>
private function createBg() {
$this->img = imagecreatetruecolor($this->width, $this->height);
$color = imagecolorallocate($this->img, mt_rand(157,255), mt_rand(157,255), mt_rand(157,255));
imagefilledrectangle($this->img,0,$this->height,$this->width,0,$color);
}
/pic/p>
private function createFont() {
$_x = $this->width / $this->codelen;
for ($i=0;$i<$this->codelen;$i++) {
$this->fontcolor = imagecolorallocate($this->img,mt_rand(0,156),mt_rand(0,156),mt_rand(0,156));
imagettftext($this->img,$this->fontsize,mt_rand(-30,30),$_x*$i+mt_rand(1,5),$this->height / 1.4,$this->fontcolor,$this->font,$this->code[$i]);
}
}
/pic/p>
private function createLine() {
/pic/p>
for ($i=0;$i<6;$i++) {
$color = imagecolorallocate($this->img,mt_rand(0,156),mt_rand(0,156),mt_rand(0,156));
imageline($this->img,mt_rand(0,$this->width),mt_rand(0,$this->height),mt_rand(0,$this->width),mt_rand(0,$this->height),$color);
}
/pic/p>
for ($i=0;$i<100;$i++) {
$color = imagecolorallocate($this->img,mt_rand(200,255),mt_rand(200,255),mt_rand(200,255));
imagestring($this->img,mt_rand(1,5),mt_rand(0,$this->width),mt_rand(0,$this->height),'*',$color);
}
}
/pic/p>
private function outPut() {
header('Content-type:image/png');
imagepng($this->img);
imagedestroy($this->img);
}
/pic/p>
public function doimg() {
$this->createBg();
$this->createCode();
$this->createLine();
$this->createFont();
$this->outPut();
}
/pic/p>
public function getCode() {
return strtolower($this->code);
}
}
輸出實例:
使用方法:
1、先把驗證碼類保存為一個名為 ValidateCode.class.php 的文件;
2、新建一個名為 captcha.php 的文件進行調(diào)用該類;
captcha.php
代碼如下:
session_start();
require './ValidateCode.class.php'; /pic/p>
$_vc = new ValidateCode(); /pic/p>
$_vc->doimg();
$_SESSION['authnum_session'] = $_vc->getCode();/pic/p>
3、引用到頁面中,代碼如下:
代碼如下:
4、一個完整的驗證頁面,代碼如下:
代碼如下:
session_start();
/pic/p>
/pic/p>
session_destroy();
/pic/p>
/pic/p>
?>
此例為session驗證實例
驗證碼:
/pic/p>
/pic/p>
";
$validate="";
if(isset($_POST["validate"])){
$validate=$_POST["validate"];
echo "您剛才輸入的是:".$_POST["validate"]."
狀態(tài):";
if($validate!=$_SESSION["authnum_session"]){
/pic/p>
echo "輸入有誤";
}else{
echo "通過驗證";
}
}
?>
【一個漂亮的php驗證碼類】相關文章:
仿照TP框架自帶的PHP驗證碼類12-29
php實現(xiàn)驗證碼制作12-24
php如何實現(xiàn)驗證碼03-15
php驗證碼代碼怎么寫08-30
用php生成帶有雪花背景的驗證碼01-08
php生成動態(tài)圖片驗證碼代碼08-11
PHP學習:PHP拼音類01-25
PHP創(chuàng)建漂亮圖表的步驟12-01
php如何在一個類中引入另外一個類02-13