php验证码无错版(无刷新更换验证码)

来源:互联网 发布:mac u盘文件不显示 编辑:程序博客网 时间:2024/05/22 00:39

为了这个验证码,在网上找了N多的资料,发现那些网上的代码不是这错误就是那错误,

还有些文章方法写得很粗,对一个新手来说,简直不知从何处下手,

我真搞不懂他们为什么要贴出来,真是误人子弟!

下面我将修改后(测试通过)的代码和方法写出来供大家参考:

 

第一步,新建一个code.php的文件,在code.php加入下面这些代码并保存

<?php
/**
*  Verification Code Class
*
*  Used to anti-spam base at PHP GD Lib
*  @author Eric,<wangyingei@yeah.net>
*  @version 1.0
*  @copyright  Ereesoft Inc. 2009
*  @update 2009-05-14 22:32:05
*  @example
*      session_sratr();
*      $vcode = new Vcode();
*      $vcode->setLength(5);
*      $_SESSION['vcode'] = $vcode->paint();// To be encrypted by MD5
*/
class Vcode{
    /**
     *  @var $width The width of the image,auto Calculated 验证图片宽度,程序自动计算
     */
    private $width;
    /**
     *  @var $height Image height 验证图片高度
     */
    private $height;
    /**
     *  @var $length Verification Code lenght 验证码长度
     */
    private $length;
    /**
     *  @var $bgColor Image background color default random 验证图片背景色
     */
    private $bgColor;
    /**
     *  @var $fontColor The text color 验证码颜色
     */
    private $fontColor;
    /**
     *  @var $dotNoise The number of noise 噪点数量
     */
    private $dotNoise;
    /**
     *  @var $lineNoise The number of noise lines 干扰线数量
     */
    private $lineNoise;
    /**
     *  @var $im image resource GD图像操作资源
     */
    private $im;
   
    /**
     *  void Vcode::Vcode()
     *
     *  The constructor
     */
    public function Vcode(){
        $this->dotNoise = 5;//初始化噪点数量
        $this->lineNoise = 0;//初始化干扰线数量
    }
     /**
     *  void Vcode::setLength(integer $length)
     *
     *  Set Verification Code length
     *  @access public
     *  @param integer $length;
     *  @return void
     */
    public function setLength($length){
        $this->length = $length;
    }
    /**
     *  void Vcode::setBgColor(string $bgColor)
     *
     *  Set background color of the Verification Image
     *  @access public
     *  @param string $bgColor e.g.: #ffffff;可以直接使用css书写中的16进制写法,但不可简写
     *  @return void
     */
    public function setBgColor($bgColor){
        $this->bgColor = sscanf($bgColor, '#%2x%2x%2x');
    }
    /**
     *  void Vcode::setFontColor(string $fontgColor)
     *
     *  Set text color of the Verification Image
     *  @access public
     *  @param string $fontColor e.g.: #ffffff;可以直接使用css书写中的16进制写法,但不可简写
     *  @return void
     */
    public function setFontColor($fontColor){
        $this->fontColor = sscanf($fontColor, '#%2x%2x%2x');
    }
    /**
     *  void Vcode::setDotNoise(integer $num)
     *
     *  How many noise dots want to draw
     *  @access public
     *  @param integer $num Too much will lower performance
     *  @return void
     */
    public function setDotNoise($num){
        $this->dotNoise = $num;//手动设置噪点数量后,会覆盖初始设置
    }
    /**
     *  void Vcode::setLineNoise(integer $num)
     *
     *  How many noise lines want to draw
     *  @access public
     *  @param integer $num Too much will lower performance
     *  @return void
     */
    public function setLineNoise($num){
        $this->lineNoise = $num;//手动设置干扰线数量后,会覆盖初始设置
    }
    /**
     *  String Vcode::randString()
     *
     *  Create Random characters 生成随机字符串
     *  @access private
     *  @return String
     */
    private function randString(){
        $string = strtoupper(md5(microtime().mt_rand(0,9)));
        return substr($string, 0, $this->length);
    }
    /**
     *  void Vcode::drawDot()
     *
     *  Draw dots noise 根据制定的数量随机画噪点,噪点颜色也随机
     *  @access private
     */
    private function drawDot(){
        for($i=0; $i<$this->dotNoise; $i++){
            $color = imagecolorallocate($this->im,
                                        mt_rand(0,255),
                                        mt_rand(0,255),
                                        mt_rand(0,255));//生成随机颜色
            imagesetpixel($this->im,
                            mt_rand(0,$this->width),
                            mt_rand(0,$this->height),
                            $color);//在随机生成的坐标上画噪点
        }
    }
    /**
     *  void Vcode::drawLine()
     *
     *  Draw line noise 随机颜色随机画干扰线
     *  @access private
     */
    private function drawLine(){
        for($i=0; $i<$this->lineNoise; $i++){
            $color = imagecolorallocate($this->im,
                                        mt_rand(0,255),
                                        mt_rand(0,255),
                                        mt_rand(0,255));//随机生成颜色
            imageline($this->im,
                        mt_rand(0,$this->width),
                        mt_rand(0,$this->height),
                        mt_rand(0,$this->width),
                        mt_rand(0,$this->height),
                        $color);//在随机生成的坐标上画干扰线
        }
    }
    /**
     *  String Vcode::paint()
     *
     *  Create image and output
     *  @access public
     *  @return string  The Verification Code to be encrypted by MD5
     */
    public function paint(){
   
        if(empty($this->length)) $this->length = 4;//验证码默认长度为4
       
        $this->width =  $this->length*12+4 ;//计算验证图片宽度
        $this->height = 20;//制定验证码图片高度
        $this->im = imagecreate($this->width, $this->height);//创建画布
       
        if(empty($this->bgColor) || empty($this->fontColor)){//如果没有设置前景色和背景色则全部随机
            //避免前景色和背景色过于接近,背景色(0-130)的随机范围与前景色(131-255)分开
            imagecolorallocate( $this->im,
                                mt_rand(0,130),
                                mt_rand(0,130),
                                mt_rand(0,130));
                               
            $randString = $this->randString();
           
            for($i=0; $i<$this->length; $i++){
                $fontColor = imagecolorallocate($this->im,
                                                mt_rand(131,255),
                                                mt_rand(131,255),
                                                mt_rand(131,255));
                imagestring($this->im, 3,
                            $i*10+8,
                            mt_rand(0,8),
                            $randString{$i},
                            $fontColor);
                            //单个验证码字符高度随机,避免被OCR
            }
           
        } else {//如果设置了背景色和前景色,则不使用随机颜色
       
            imagecolorallocate( $this->im,
                                $this->bgColor[0],
                                $this->bgColor[1],
                                $this->bgColor[2]);
            $randString = $this->randString();
            $fontColor = imagecolorallocate($this->im,
                                            $this->fontColor[0],
                                            $this->fontColor[1],
                                            $this->fontColor[2]);
            for($i=0;$i<$this->length;$i++){
                imagestring($this->im, 3,
                            $i*10+8,
                            mt_rand(0,8),
                            $randString{$i},
                            $fontColor);//每个验证码字符高度仍然随机
            }
           
        }
       
        $this->drawDot();//绘制噪点
        $this->drawLine();//绘制干扰线
        imagepng($this->im);
        imagedestroy($this->im);
        return md5($randString);//返回MD5加密后的验证码,可直接放入session
       
    }
}

session_start();
$vcode = new Vcode();
$vcode->setLength(5);
$_SESSION['vcode']=$vcode->paint();// To be encrypted by MD5
?>

 

 

 

第二步:在登陆页面login.php中调用验证码(注意:login.php和code.php 需放到同一个目录里面)

//login.php里面的代码如下:

<form id="form1" name="form1" method="post" action="test.php">

// test.php为提交后的验证页面
  <input name="yzm" type="text" class="STYLE1" id="yzm" />
  <img src="code.php?image" onclick="this.src='code.php?image'"/>
  <input name="Submit" type="submit" id="Submit" value="submit" />
</form>

 

 

最后一步:开始验证输入的验证码是否正确,如果不正确,则返回登录页面

//创建一个test.php的测试页面,里面的代码如下

<?php
session_start();
//在页首先要开启session,
//error_reporting(2047);
session_destroy();

if($_POST[Submit]){
  if(md5($_POST[yzm])==$_SESSION['vcode']){
    echo "";
  }else{
     echo '<script>alert("验证码不正确,请重新输入!"); window.location.href="login.php"; </script>';
  }
}
?>

 

 

到此结束,百分百有效,绝不会出现乱码!

原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 护士资格证延续注册过期了怎么办 护士资格证过期没注册怎么办 护士资格证注册时间过期怎么办 辅警体检视力不行怎么办 护士延续注册体检怀孕怎么办 护士资格证没有延续注册怎么办 申请信用卡没有座机号码怎么办 网上申请信用卡没有座机号码怎么办 我叫上门服务被骗了怎么办 上门服务被骗了3000多怎么办 微信被骗9000元怎么办 奥迪a8气囊灯亮怎么办 驾考站岗迟到了怎么办 老板欠员工工资不给怎么办 如果有一天我没头发了怎么办 苏州公积金密码忘了怎么办 科二考试第二把怎么办 科一老是记不住怎么办 科目二考试没去怎么办 网约车驾龄不到怎么办 科四预约不上怎么办 教练不退钱怎么办找谁 驾考出入证丢了怎么办 科二成绩单丢了怎么办 考驾照的准考证丢了怎么办 驾考预约去不了怎么办 科目一预约没去怎么办 打狂犬疫苗期间感冒了怎么办 公司社保欠费不交怎么办 25号社保不交怎么办欠费 会计从业停考了怎么办 黑龙江龙育黄了档案怎么办 科目四档案丢了怎么办 从上海调档案到杭州怎么办 户口迁移身份证变更护照怎么办 有中国签证的孩子怎么办户口 大学生户口在学校怎么办签证 户口在南方人才市场怎么办签证 报警电话接到说方言的怎么办 学生去新加坡旅游签证怎么办 出入境的受理编号不见了怎么办