php验证码的制作

现在一般的网站,类似于用户验证之类的都会有验证码验证。就我个人的理解,验证码主要的作用是防止木马之类的恶意登录。下面主要介绍一下利用php语言来制作一个简单的验证码。运行效果如下:

这个验证码主要实现了,产生一个字符和数字随机组合的字符串输出到图片上,点击图片实现验证码切换,并且产生干扰线。使用干扰线的主要目的是防止一些程序通过扫描自动填写验证码。

首先在php.ini中开启gd2扩展,也就是去掉前面的分号,并重启服务器。

下面的工具类实现了验证码的输出

<?php
/**封装验证码工具
 * Class captohaLib
 */
class captchaLib{
    private $length;  //验证码长度
    private $fontSize;//内置字体大小
    //构造函数初始化变量
    public function __construct($length=4,$fontSize=5){
        $this->length=$length;
        $this->fontSize=$fontSize;
    }
    //生成随机字符串
    private function generalCode(){
        //生成有数字和字母组成的数组
        $char_array=array_merge(range('a','z'),range(0,9),range('A','Z'));
        //打乱数组下标
        shuffle($char_array);
        //从数组中随机去四个字符,返回数组下标
        $indexes=array_rand($char_array,$this->length);
        //拼接字符串
        $str='';
        foreach ($indexes as $index) {
            $str.=$char_array[$index];
        }
        return  $str;
    }
    //生成验证码
    public function generalCapctha(){
        $str=$this->generalCode();  //获取字符串
        $_SESSION['code']=$str;   //将验证码放入session中,以便校验
        //设置背景图地址
        $bg_path=LIB_PATH.'captcha/captcha_bg'.rand(1,5).'.jpg';
        //打开图片
        $img=imagecreatefromjpeg($bg_path);
        //定义前景色
        $frontColor=imagecolorallocate($img,0,0,0);
        switch (rand(1,5)){
            case 1:
                $frontColor=imagecolorallocate($img,0,0,0);
                break;
            case 2:
                $frontColor=imagecolorallocate($img,255,255,255);
                break;
            case 3:
                $frontColor=imagecolorallocate($img,255,0,0);
                break;
            case 4:
                $frontColor=imagecolorallocate($img,0,255,0);
                break;
            case 5:
                $frontColor=imagecolorallocate($img,0,0,255);
                break;
        }
        //将字符串写到图片上(居中显示)
        $x=(imagesx($img)-imagefontwidth($this->fontSize)*strlen($str))/2; //设置x坐标
        $y=(imagesy($img)-imagefontheight($this->fontSize))/2; //设置y坐标
        imagestring($img,$this->fontSize,$x,$y,$str,$frontColor);
        //给图片设置干扰线
        $x1=0; //干扰线X坐标起点
        $x2=imagesx($img); //干扰线X坐标终点
        $str_y=(imagesy($img)-imagefontheight($this->fontSize))/2;
        //绘制干扰线
        for ($i=1;$i<=2;$i++){
            $y1=rand($str_y,imagefontheight($this->fontSize)); //干扰线y坐标起点
            $y2=rand($str_y,imagefontheight($this->fontSize)); //干扰线y坐标终点
            imageline($img,$x1,$y1,$x2,$y2,$frontColor);
        }
        ob_clean(); //手动清空缓存
        //设置图片解释格式
        header('content-type:image/png');
        //输出图片
        imagepng($img);
        //销毁资源
        imagedestroy($img);
    }
    //检查验证码是否正确
    public function checkCaptcha($code){
        return $code==$_SESSION['code'];
    }

}

其中需要注意的是,$_SESSION['code']=$str;把产生的字符串存放在session中,以便做后台的验证,做后台验证很简单,就是获取前台输入的验证码跟session中存放的验证码进行比较,代码如下:

if (!empty($_POST)) {
            //检查验证码
            $captcha=$_POST['captcha'];
            $c=new captchaLib();
            if (!$c->checkCaptcha($captcha)){
                $this->error('index.php?p=admin&$c=login&$a=login','验证码错误',3);
                exit;
            }
            ......
}
......
//获取验证码
 public function getGeneralCaptchaAction(){
      $captcha=new captchaLib();
      $captcha->generalCapctha();
 }

点击图片切换验证码代码如下,其中Math.random();是产生随机数字进行强制刷新:

<img src="index.php?p=admin&c=login&a=getGeneralCaptcha"
         onclick="this.src='index.php?p=admin&c=login&a=getGeneralCaptcha&'+Math.random();"
         alt="" width="145" height="20">

初学php验证码时会遇到一些很奇怪又不知道如何解决的错误,对于初学者来说确实是一个难题,就像如下的错误,没有具体的错误提示

验证码 出错时的解决办法,参照https://blog.csdn.net/weixin_40391011/article/details/84451768..

发布了28 篇原创文章 · 获赞 5 · 访问量 1200

猜你喜欢

转载自blog.csdn.net/weixin_40391011/article/details/84348119