此文章由【网友-微笑
】提供:
此验证码一共有3个文件:
1、functions.php
2、code.config.php
3、code.class.php
代码如下:
<?php
/*
*
*/
//获得数据库模型的对象
function M($table){
return new Model($table);
}
//打印信息
function p($msg){
echo "<pre>";
print_r($msg);
echo "</pre>";
}
//提示错误信息
function error($msg){
echo "<div align='center' style='border:solid 3px #dcdcdc;padding:20px;margin:0 auto;'>
$msg
</div>";
exit;
}
//自动加载类文件
function __autoload($className){
if(strlen($className)>7 && substr($className,-7) == 'Control'){
$classFile = PATH_APP.'/control/'.$className.'.php';
}else{
$classFile = PATH_XL.'/libs/bin/'.$className.'.class.php';
}
if(!is_file($classFile)){
exit($classFile."文件不存在");
}
include $classFile;
}
//加载、设置配置项
function C($name=null,$value=null){
static $config =array();
if(is_array($name)){
$config = array_merge($config,array_change_key_case($name));
}
if(is_string($name)){
$name = strtolower($name);
if(!is_null($value)){
$congfig[$name]=$value;
return true;
}
return isset($config[$name])?$config[$name]:null;
}
if(is_null($name)){
return $config;
}
}
//加载、设置字体
function L($name=null,$value=null){
static $lang = array();
if(is_array($name)){
$lang = array_merge($lang,array_change_key_case($name));
}
if(is_string($name)){
$name = strtolower($name);
if(!is_null($value)){
$lang[$name]=$value;
return true;
}
return isset($lang[$name])?$lang[$name]:null;
}
if(is_null($name)){
return $lang;
}
}
//获得文件、文件夹的大小
function getsize($path,$type=null){
if(is_file($path)) return filesize($path);
$type = is_null($type)?"*":"*.$type";
$files = glob($path.'/'.$type);
$size = 0;
foreach($files as $f){
$size+=is_dir($f)?getsize($f):filesize($f);
}
return $size;
}
?>
<?php
return array(
//验证码配置项
'code_str'=>'qwertyuiopasdfghjklzxcvbnm1234567890',//种子
'code_length'=>5,//长度
'code_width'=>'105',//宽度
'code_height'=>'30',//高度
'code_bg_color'=>'#cccccc',//背景颜色
'code_font_color'=>'#ff0000',//字体颜色
'code_font'=>'./MSYHBD.TTF',//字体样式
'code_size'=>'16',//字体大小
);
?>
<?php
session_start();
include "functions.php";
C(include "code.config.php");
//验证码类
class code{
public $code_str;//种子
public $code_length;//长度
public $code_width;//宽度
public $code_height;//高度
public $code_bg_color;//背景颜色
public $code_font_color;//字体颜色
public $code_font;//字体样式
public $code_size;//字体大小
public $res;//资源
//构造函数
function __construct($width=null,$height=null,$length=null,$ftcolor=null,$bgcolor=null){
//初始化部分属性
$this->code_width = is_null($width)?C('code_width'):$width;
$this->code_height = is_null($height)?C('code_height'):$height;
$this->code_length = is_null($length)?C('code_length'):$length;
$this->code_font_color = is_null($ftcolor)?C('code_font_color'):$ftcolor;
$this->code_bg_color = is_null($bgcolor)?C('code_bg_color'):$bgcolor;
$this->code_font = C('code_font');
$this->code_size = C('code_size');
}
//提供生成验证码的调用函数
public function show(){
$this->codecreate();//生成验证码
header('Content-type:image/png');
imagepng($this->res);
}
//生成验证码
private function codecreate(){
if(!$this->checkGD()){//检测图像环境
//error();
return false;
}
//画布资源
$this->res = imagecreatetruecolor($this->code_width,$this->code_height);
//填充画布背景色
$bgArr = $this->getcolor($this->code_bg_color);
$bgcolor = imagecolorallocate($this->res,$bgArr[0],$bgArr[1],$bgArr[2]);
imagefill($this->res,0,0,$bgcolor);
//获得验证码文字内容
$str = '';
$str = $this->getcode();
//向画布上写入验证码、获得每个字符的宽度
$width = ($this->code_width-5)/$this->code_length;
//获得文字颜色的十进制表示
$setColor = false;
if($this->code_font_color){
$fontArr = $this->getcolor($this->code_font_color);
$color = imagecolorallocate($this->res,$fontArr[0],$fontArr[1],$fontArr[2]);
$setColor = true;
}
for($i=0;$i<$this->code_length;$i++){
$x = $i*$width+5;
if(!$setColor){
$color = imagecolorallocate($this->res,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
}
imagettftext($this->res,$this->code_size,mt_rand(-25,25),$x,mt_rand($this->code_height/1.6,$this->code_height-5),$color,$this->code_font,$str[$i]);
}
//绘制干扰
$this->createdisturb($color);
}
//检测图像环境
private function checkGD(){
return extension_loaded('GD') && function_exists('imagepng');
}
//获得颜色的十进制
private function getcolor($color){
$color = trim($color,'#');
$arr = array();
$arr[0] = hexdec(substr($color,0,2));
$arr[1] = hexdec(substr($color,2,2));
$arr[2] = hexdec(substr($color,4,2));
return $arr;
}
//获得验证码文字内容
private function getcode(){
$len = $this->code_length;
$this->code_str = C('code_str');
$code = '';
for($i=0;$i<$len;$i++){
$index = mt_rand(0,strlen($this->code_str)-1);
$code.= $this->code_str[$index];
}
$code = strtoupper($code);
$_SESSION['code'] = $code;
return $code;
}
//绘制干扰
private function createdisturb($color){
$w = $this->code_width;
$h = $this->code_height;
//绘制干扰点
for($i=0;$i<100;$i++){
imagesetpixel($this->res,mt_rand(0,$w),mt_rand(0,$h),$color);
}
//绘制干扰线
for($i=0;$i<3;$i++){
imageline($this->res,mt_rand(0,$w),mt_rand(0,$h),mt_rand(0,$w),mt_rand(0,$h),$color);
}
}
}
//new一个验证码的类 执行显示验证码的方法即可
$x = new code();
$x->show();
?>
如果您觉得本文的内容对您的学习有所帮助:
关键字:
PHP验证码