【www.gdgbn.com--php常用代码】

session_start();
class authnum {
 //图片对象、宽度、高度、验证码长度
 private $im;
 private $im_width;
 private $im_height;
 private $len;
 //随机字符串、y轴坐标值、随机颜色
 private $randnum;
 private $y;
 private $randcolor;
 //背景色的红绿蓝,默认是浅灰色
 public $red=238;
 public $green=238;
 public $blue=238;
 /**
  * 可选设置:验证码类型、干扰点、干扰线、y轴随机
  * 设为 false 表示不启用
 **/
 //默认是大小写数字混合型,1 2 3 分别表示 小写、大写、数字型
 public $ext_num_type="";
 public $ext_pixel = false; //干扰点
 public $ext_line  = false; //干扰线
 public $ext_rand_y= true;  //y轴随机
 function __construct ($len=4,$im_width="",$im_height=25) {
  // 验证码长度、图片宽度、高度是实例化类时必需的数据
  $this->len      = $len; $im_width = $len * 15;
  $this->im_width = $im_width;
  $this->im_height= $im_height;
  $this->im = imagecreate($im_width,$im_height);
 }
 // 设置图片背景颜色,默认是浅灰色背景
 function set_bgcolor () {
  imagecolorallocate($this->im,$this->red,$this->green,$this->blue);
 }
 // 获得任意位数的随机码
 function get_randnum () {
  $an1 = "abcdefghijklmnopqrstuvwxyz";
  $an2 = "abcdefghijklmnopqrstuvwxyz";
  $an3 = "0123456789";
  if ($this->ext_num_type == "")  $str = $an1.$an2.$an3;
  if ($this->ext_num_type == 1) $str = $an1;
  if ($this->ext_num_type == 2) $str = $an2;
  if ($this->ext_num_type == 3) $str = $an3;
  for ($i = 0; $i < $this->len; $i++) {
   $start = rand(1,strlen($str) - 1);
   $randnum .= substr($str,$start,1);
  }
  $this->randnum = $randnum;
  $_session[an] = $this->randnum;
 }
 // 获得验证码图片y轴
 function get_y () {
  if ($this->ext_rand_y) $this->y = rand(5, $this->im_height/5);
  else $this->y = $this->im_height / 4 ;
 }
 // 获得随机色
 function get_randcolor () {
  $this->randcolor = imagecolorallocate($this->im,rand(0,100),rand(0,150),rand(0,200));
 }
 // 添加干扰点
 function set_ext_pixel () {
  if ($this->ext_pixel) {
   for($i = 0; $i < 100; $i++){
       $this->get_randcolor();
       imagesetpixel($this->im, rand()%100, rand()%100, $this->randcolor);
   }
  }
 }
 // 添加干扰线
 function set_ext_line () {
  if ($this->ext_line) {
   for($j = 0; $j < 2; $j++){
    $rand_x = rand(2, $this->im_width);
    $rand_y = rand(2, $this->im_height);
    $rand_x2 = rand(2, $this->im_width);
    $rand_y2 = rand(2, $this->im_height);
    $this->get_randcolor();
    imageline($this->im, $rand_x, $rand_y, $rand_x2, $rand_y2, $this->randcolor);
   }
  }
 }
 /**创建验证码图像:
  * 建立画布(__construct函数)
  * 设置画布背景($this->set_bgcolor();)
  * 获取随机字符串($this->get_randnum ();)
  * 文字写到图片上(imagestring函数)
  * 添加干扰点/线($this->set_ext_line(); $this->set_ext_pixel();)
  * 输出图片
 **/
 function create () {
  $this->set_bgcolor();
  $this->get_randnum ();
  for($i = 0; $i < $this->len; $i++){
   $font = rand(4,6);
      $x    = $i/$this->len * $this->im_width + rand(1, $this->len);
   $this->get_y();
   $this->get_randcolor();
      imagestring($this->im, $font, $x, $this->y, substr($this->randnum, $i ,1), $this->randcolor);
  }
      $this->set_ext_line();
      $this->set_ext_pixel();
   header("content-type:image/png");
   imagepng($this->im);
   imagedestroy($this->im);     //释放图像资源
 }

}//end class
/**使用验证码类的方法:
 * $an = new authnum(验证码长度,图片宽度,图片高度);
 * 实例化时不带参数则默认是四位的60*25尺寸的常规验证码图片
 * 表单页面检测验证码的方法,对比 $_session[an] 是否等于 $_post[验证码文本框id]
 * 可选配置:
 *  1.验证码类型:$an->ext_num_type=1;  值为1是小写类型,2是大写类型,3是数字类型
 *  2.干扰点:$an->ext_pixel = false;   值为false表示不添加干扰点
 *  3.干扰线:$an->ext_line = false;    值为false表示不添加干扰线
 *  4.y轴随机:$an->ext_rand_y = false; 值为false表示不支持图片y轴随机
 *  5.图片背景:改变 $red $green $blue 三个成员变量的值即可
**/
$an = new authnum();
$an->ext_num_type="";
$an->ext_pixel = true; //干扰点
$an->ext_line  = false; //干扰线
$an->ext_rand_y= true; //y轴随机
$an->green = 238;
$an->create();
?>

好下面来看一款验证码调用实例

例子demo:

以下为引用的内容:




hi.baidu.com/ji_haiyang




 


例子demo1:

session_start();
$test = $_post["test"];
$test = trim($test);
$submit = $_post["submit"];
if($test==$_session["vcode"]){
echo "true,输入验证码正确";
} else {
echo "false,输入验证码错误";
}

?>
 

调用文件vcode.php: 以下为引用的内容:
/**
* 默认验证码session为vcode.即:$_session["vcode"];
* 注意在给变量符值时不要把变量的名子和session冲突
* 注:在验证时不分大小写
*/
include("inc_vcode_class.php");
$v = new vcode;
//$config["width"] = 50;   //验证码宽
//$config["height"] = 20;   //验证码高
//$config["vcode"] = "vcode"; //检查验证码时用的session
//$config["type"] = "default"; //验证码展示的类型default:大写字母,string:小写字母,int:数字 php程序员站
//$config["length"] = 4;   //验证码长度
//$config["interfere"]= 10;   //干扰线强度,范围为1-30,0或空为不起用干扰线
//$v->init($config); //配置
$v->create();
?>

验证码类inc_vcode_class.php: www~phperz~com 以下为引用的内容:
/**
* 验证码类
* 注:需要gd库支持
*/
session_start();
class vcode{
private $config;
private $im;
private $str;

function __construct(){
$this->config["width"] = 50;
$this->config["height"] = 20;
$this->config["vcode"] = "vcode";
$this->config["type"] = "default";
$this->config["length"] = 4;
$this->config["interfere"] = 10;

$this->str["default"] = "abcdefghijklmnopqrstuvwxyz";
$this->str["string"] = "abcdefghijklmnopqrstuvwxyz";
$this->str["int"]  = "0123456789";
}

//配置
public function init($config=array()){
if (!empty($config) && is_array($config)){
foreach($config as $key=>$value){
$this->config[$key] = $value;
}
}
}

//输出验证码
public function create(){
if (!function_exists("imagecreate")){
return false;
}
$this->create_do();
}

//创建
private function create_do(){
$this->im = imagecreate($this->config["width"],$this->config["height"]); php程序员之家
//设置背景为白色
imagecolorallocate($this->im, 255, 255, 255);

//为此背景加个边框
$bordercolor= imagecolorallocate($this->im,37,37,37);
imagerectangle($this->im,0,0,$this->config["width"]-1,$this->config["height"]-1,$bordercolor);

//生成验证码
$this->create_str();
$vcode = $_session[$this->config["vcode"]];

//输入文字
$fontcolor = imagecolorallocate($this->im,46,46,46);
for($i=0;$i<$this->config["length"];$i++){
imagestring($this->im,5,$i*10+6,rand(2,5),$vcode[$i],$fontcolor);
}

//加入干扰线
$interfere = $this->config["interfere"];
$interfere = $interfere>30?"30":$interfere; php程序员站
if (!empty($interfere) && $interfere>1){
for($i=1;$i<$interfere;$i++){
$linecolor = imagecolorallocate($this->im,rand(0,255),rand(0,255),rand(0,255));
$x = rand(1,$this->config["width"]);
$y = rand(1,$this->config["height"]);
$x2 = rand($x-10,$x+10);
$y2 = rand($y-10,$y+10);
imageline($this->im,$x,$y,$x2,$y2,$linecolor);
}
}

header("pragma:no-cachern");
header("cache-control:no-cachern");
header("expires:0rn");
header("content-type:image/jpegrn");
imagejpeg($this->im);
imagedestroy($this->im);
exit;
}

//得到验证码
private function create_str(){
if ($this->config["type"]=="int"){
for($i=1;$i<=$this->config["length"];$i++){
$vcode .= rand(0,9);
}
$_session[$this->config["vcode"]] = $vcode;
return true;
}
$len = strlen($this->str[$this->config["type"]]);
if (!$len){
$this->config["type"] = "default";
$this->create_str();
}
for($i=1;$i<=$this->config["length"];$i++){
$offset  = rand(0,$len-1);
$vcode .= substr($this->str[$this->config["type"]],$offset,1);
}
$_session[$this->config["vcode"]] = $vcode;
return true;
}
}

本文来源:http://www.gdgbn.com/jiaocheng/28261/