PHP实现图片登录验证码的解决方案

简介: PHP实现图片登录验证码的解决方案

PHP实现图片登录验证码的解决方案

HTML代码

    <div class="login">
        <p><input type="text" id="username" name="username" placeholder="用户名" autocomplete="off"></p>
        <p><input type="password" id="password" name="username" placeholder="密码" autocomplete="off"></p>
        <p><input type="text" id="captcha" name="captcha" placeholder="验证码" maxlength="4" autocomplete="off"></p>
        <p><img src="?m=Login&a=loginAccess&act=getCode" id="getCode" alt="" title="点击刷新验证码"></p>
        <p><button id="loginBtn">登录</button></p>
    </div>


刷新图片

        $("#getCode").click(function () {
            $(this).attr("src", '?m=Login&a=loginAccess&act=getCode&' + Math.random());
        });


生成验证码和图片

    case "getCode";
        require_once "libs/vcode.class.php";
        $obj = new vcode();//实例化;
        SetCookie("authcode", $obj->authcode, time() + 30, "/");
        die($obj->output());
        break;


验证码类库

class vcode
{
    public $authcode = '';                            //验证码
    private $width = '';                            //验证码图片宽
    private $height = '';                            //验证码图片高
    private $len = '';                            //验证码长度
    private $tilt = array(-30, 30);                //验证码倾斜角度
    private $font = 'AlteHaasGroteskBold.ttf';    //字体文件
    private $str = '';                            //验证码基
    private $im = '';                            //生成图片的句柄
    //构造函数,生成验证码。
    function __construct($width = 100, $heigh = 40, $len = 4)
    {
        $this->width = $width;
        $this->height = $heigh;
        $this->len = $len;
        //$this->str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
        $this->str = '0123456789';
        $str_len = strlen($this->str) - 1;
        for ($i = 0; $i < $len; $i++) {
            $this->authcode .= $this->str[rand(0, $str_len)];
        }
    }
    //创建图片
    private function imagecreate()
    {
        $this->im = imagecreatetruecolor($this->width, $this->height);
    }
    //干扰颜色
    private function ext_color()
    {
        return imagecolorallocate($this->im, rand(50, 180), rand(50, 180), rand(50, 180));
    }
    //创建干扰点
    private function ext_point()
    {
        for ($i = 0; $i < $this->width * 2; $i++) {
            imagesetpixel($this->im, rand(1, $this->width - 1), rand(1, $this->height - 1), $this->ext_color());
        }
    }
    //创建干扰线
    private function ext_line()
    {
        for ($i = 0; $i < $this->len; $i++) {
            $x1 = rand(1, $this->width - 1);
            $y1 = rand(1, $this->height - 1);
            $x2 = rand(1, $this->width - 1);
            $y2 = rand(1, $this->height - 1);
            imageline($this->im, $x1, $y1, $x2, $y2, $this->ext_color());
        }
    }
    //把验证码写入图片(不能和$this->imgstrfloat()同时使用)
    private function imgstr()
    {
        $old_x = 1;
        for ($i = 0; $i < $this->len; $i++) {
            $fontsize = rand(2, 5);        //字体大小
            $tmp_1 = $fontsize * 2.5;
            $tmp_2 = $i > 0 ? $tmp_1 : 0;
            $y = rand(1, $this->height / 2);
            $x = rand($old_x + $tmp_2, ($i + 1) * ($this->width) / $this->len - $tmp_1);
            $old_x = $x;
            $color = imagecolorallocate($this->im, rand(200, 255), rand(200, 255), rand(200, 255));
            imagestring($this->im, $fontsize, $x, $y, $this->authcode[$i], $color);
        }
    }
    //把验证码倾斜写入图片(不能和$this->imgstr()同时使用)
    private function imgstrfloat()
    {
        $old_x = 1;
        for ($i = 0; $i < $this->len; $i++) {
            $fontfloat = rand($this->tilt[0], $this->tilt[1]);
            $fontsize = rand(10, 15);        //字体大小
            $tmp_1 = $i > 0 ? $fontsize : 0;
            $y = rand($fontsize + 2, $this->height - 2);
            $x = rand($old_x + $tmp_1 + 2, ($i + 1) * ($this->width) / $this->len - $fontsize - 2);
            $old_x = $x;
            $color = imagecolorallocate($this->im, rand(200, 255), rand(200, 255), rand(200, 255));
            imagettftext($this->im, $fontsize, $fontfloat, $x, $y, $color, $this->font, $this->authcode[$i]);
        }
    }
    //输出图片
    public function output()
    {
        $this->imagecreate();
        $this->imgstr();
        //$this->imgstrfloat();
        $this->ext_point();
        $this->ext_line();
        header('content-type:image/png');
        imagepng($this->im);
        imagedestroy($this->im);
    }
}


@lockdata.cn

相关文章
|
设计模式 数据库连接 PHP
PHP中的设计模式:提升代码的可维护性与扩展性在软件开发过程中,设计模式是开发者们经常用到的工具之一。它们提供了经过验证的解决方案,可以帮助我们解决常见的软件设计问题。本文将介绍PHP中常用的设计模式,以及如何利用这些模式来提高代码的可维护性和扩展性。我们将从基础的设计模式入手,逐步深入到更复杂的应用场景。通过实际案例分析,读者可以更好地理解如何在PHP开发中应用这些设计模式,从而写出更加高效、灵活和易于维护的代码。
本文探讨了PHP中常用的设计模式及其在实际项目中的应用。内容涵盖设计模式的基本概念、分类和具体使用场景,重点介绍了单例模式、工厂模式和观察者模式等常见模式。通过具体的代码示例,展示了如何在PHP项目中有效利用设计模式来提升代码的可维护性和扩展性。文章还讨论了设计模式的选择原则和注意事项,帮助开发者在不同情境下做出最佳决策。
|
JavaScript 前端开发 安全
php学习笔记-普通表单参数提交获取及页面的重定向和一个登录小demo-day05
本文介绍了PHP中普通表单参数的提交获取、页面重定向的方法,并通过一个登录示例演示了表单参数的封装和页面跳转处理。
|
11月前
|
缓存 Java 应用服务中间件
java语言后台管理若依框架-登录提示404-接口异常-系统接口404异常如何处理-登录验证码不显示prod-api/captchaImage 404 (Not Found) 如何处理-解决方案优雅草卓伊凡
java语言后台管理若依框架-登录提示404-接口异常-系统接口404异常如何处理-登录验证码不显示prod-api/captchaImage 404 (Not Found) 如何处理-解决方案优雅草卓伊凡
2388 5
|
缓存 NoSQL PHP
Redis作为PHP缓存解决方案的优势、实现方式及注意事项。Redis凭借其高性能、丰富的数据结构、数据持久化和分布式支持等特点,在提升应用响应速度和处理能力方面表现突出
本文深入探讨了Redis作为PHP缓存解决方案的优势、实现方式及注意事项。Redis凭借其高性能、丰富的数据结构、数据持久化和分布式支持等特点,在提升应用响应速度和处理能力方面表现突出。文章还介绍了Redis在页面缓存、数据缓存和会话缓存等应用场景中的使用,并强调了缓存数据一致性、过期时间设置、容量控制和安全问题的重要性。
278 5
|
XML 前端开发 JavaScript
php中Ajax的简单使用,登录表单调用Ajax判断是否正确登录利用layer.msg进行提示
本文介绍了在PHP中如何使用Ajax进行登录表单的数据提交,并利用jQuery的$.post()方法与后端通信,以及使用layer.msg进行前端提示。
php中Ajax的简单使用,登录表单调用Ajax判断是否正确登录利用layer.msg进行提示
|
资源调度 JavaScript API
nest.js + sms 实现短信验证码登录
本文介绍了在Nest.js框架中集成短信验证码登录的实现方案,详细阐述了使用阿里云短信服务的配置流程、资质申请、短信模板设置,并提供了API调用示例和工程代码的运行步骤。
nest.js + sms 实现短信验证码登录
|
C#
C# 图形验证码实现登录校验代码
C# 图形验证码实现登录校验代码
365 2
|
Java
Java 登录输入的验证码
Java 登录输入的验证码
181 1
|
数据采集 自然语言处理 Python
用 Python 生成并识别图片验证码
用 Python 生成并识别图片验证码
404 1
|
存储 JSON 前端开发
node使用token来实现前端验证码和登录功能详细流程[供参考]=‘很值得‘
本文介绍了在Node.js中使用token实现前端验证码和登录功能的详细流程,包括生成验证码、账号密码验证以及token验证和过期处理。
516 0
node使用token来实现前端验证码和登录功能详细流程[供参考]=‘很值得‘