php的验证码代码

谁能提供详细又完整的又没有错误的验证代码?
百度和gole的代码没有一个正确的。郁闷死

<?php
/**
* 类说明:
* 使用时,可按自己的需要设置输出图片的宽度和高度,以及要产生的验证码个数和干扰部分;
* 使用时,只需要将validate实例化,然后调用show_image()可生即可生成验证码。
* 获取验证码的方法是在其它页面 首先开户session_start(),然后直接使用$_SESSION['code']即可;
* 注意,大多数新手可能会遇到一个问题,就是$_SESSION['code']的值总是要慢一拍,用户在输入验证码点提交后,
* session的值才会被刷新,这样使用不会有错,如果直接用JS去获取得取到的是上次的产生的.
* 最后:该类由游天小虾制作,您可以不保留此信息,可任意传播,如果您对本类有什么提意,
* 可发关邮件到:[email protected]
* 或者加入我们的网页制作交流群(聚义堂) 69574955
* **/
class validate {
private $width = '80';//验证码的宽度
private $height = '20';//验证码的高度
private $randcode = '';//验证码, 无需赋值,后面会随机生成
private $num = '4';//验证码的字数
private $interferon = '80';//干扰素数量
private $line ='2';//线条干扰条数
private $im = '';//无需赋值,图片自动生成/**
* 输入网页类型
* */
private function conten_type(){
header("Content_type:image/gif");
}
/***
*打开session
* **/
private function session_star(){
session_start();
}/**
* 产生随机数
* **/
private function random(){
$this->randcode = strtoupper(substr(md5(rand()),0,$this->num));
return $this->randcode;
}
/**
* 置障session的值
* **/
private function resession(){
$_SESSION['code'] = $this->randcode;
}
/**
* 产生验证图片
***/
private function create_image(){
$this->im = imagecreate($this->width,$this->height);
imagecolorallocate ($this->im, rand(50,60), rand(150,200),rand(230,250));
return $this->im;
} /**
* 产生干扰素
* **/
private function create_interferon(){
for($i=0;$i<$this->interferon;$i++){
$infcolor = imagecolorallocate($this->im,rand(0,255),rand(0,255),rand(0,255));
imagesetpixel($this->im,rand(0,80),rand(0,20),$infcolor);
} } /**
* 产生干扰线条
* **/
private function create_line(){
for($j=0;$j<$this->line;$j++){
$lineColor = imagecolorallocate($this->im,rand(0,255),rand(0,255),rand(0,255));
imageline($this->im,rand(0,80),rand(0,20),rand(0,80),rand(0,20),$lineColor);
}
} /**
* 写入字符
* **/
private function read_text(){
for($i=0;$i<$this->num;$i++){
$textColor = imagecolorallocate($this->im,rand(0,100),rand(0,100),rand(0,100));
$x = rand(($this->width/$this->num*$i),($this->width/$this->num)*($i+1)-10);
$y = rand(2,$this->height-13);
imagechar($this->im,rand(4,5),$x,$y,$this->randcode[$i],$textColor);
} } /**
* 输出验证码图片
* **/
public function show_image(){
$this->session_star();
$this->conten_type();
$this->random();
$this->resession();
$this->create_image();
$this->create_interferon();
$this->create_line();
$this->read_text();
imagepng($this->im);
imagedestroy($this->im);
}} $va = new validate(); $va->show_image();
?>
温馨提示:答案为网友推荐,仅供参考