看了一些比較大的體驗比較好的網站驗證碼都較為簡單
有什麼地方寫的不好的歡迎大家批評
[php]
if (!defined(
/*********************************************************************************
* InitPHP
*
*
* $Author:liuxinming
* $Dtime:
***********************************************************************************/
class seccodeInit{
private $width;
private $height;
private $type=
private $time=
private $color=null;//驗證碼字體顏色
private $im;
private $length=
private $warping;//隨機扭曲
/**
* 獲取隨機數值
* @return string
*/
private function get_random_val() {
$i=
while($i<$this
{
mt_srand((double)microtime()*
$randnum=mt_rand(
if(!in_array($randnum
{
$authnum=$authnum
$i++;
}
}
session_start();
$time=time();
$checkcode=md
$key=$time
$_SESSION[
return $authnum;
}
/**
* 獲取驗證碼圖片
* @param $width 寬
* @param $height 高
* @param $warping 字體隨機扭曲開關
* @return string
*/
public function getcode($width=
$this
$this
$this
if($this
function_exists(
$this
}
}
/**
* 生成圖片驗證碼
* @return string
*/
public function image(){
$this
imagecolorallocate($this
$this
$ttfPath = dirname(__FILE__)
$dirs = opendir($ttfPath);
$seccodettf = array();
while($entry = readdir($dirs)) {
if($entry !=
$seccodettf[] = $ttfPath
}
}
$ttf = $seccodettf[array_rand($seccodettf)];//隨機一種字體
$size = $this
imagettftext($this
if($this
$this
}
if(function_exists(
{
header (
$code=imagepng($this
}elseif (function_exists(
{
header (
$code=imagejpeg($this
}elseif (function_exists(
{
header(
$code=imagegif($this
}
imagedestroy($this
return $code;
}
/**
* 檢查驗證碼
* @param $code
* @return bool
*/
public function checkCode($code) {
session_start();
$secode=explode(
$time=time();
//檢查時間是否過期
if($secode[
{
return false;
}
//驗證碼密鑰 雙md
if($secode[
return false;
}
//檢查驗證碼字符串是否一致
if($code||$code<>$secode[
{
return false;
}
return true;
}
/*隨機扭曲*/
public function setWarping(){
$rgb=array();
$direct=rand(
$width = imagesx($this
$height = imagesy($this
$level=$width /
for($j =
for($i =
$rgb[$i] = imagecolorat($this
}
for($i =
$r = sin($j / $height *
imagesetpixel($this
}
}
}
}
?>
效果
From:http://tw.wingwit.com/Article/program/PHP/201311/20786.html