搜索
首页类库下载PHP类库PHP生成图片验证码demo【OOP面向对象版本】

PHP生成图片验证码demo【OOP面向对象版本】

下面是我今天下午用PHP写的一个生成图片验证码demo,仅供参考。

这个demo总共分为4个文件,具体代码如下:

1、code.html中的代码:

<!doctype html>
<html>
    <head>
        <meta charset="utf-8" />
        <title>登录、注册验证码生成</title>
    </head>
    <body>
         <!--
             * @Description  网站登录/注册验证码生成类
             * @Author  赵一鸣
             * @OnlineDemo http://www.zymseo.com/demo/verificationcode/code.html
             * @Date  2016年10月6日 
         -->
        <form action="checkcode.php" method="post">
            <input type="text" name="code" /><br/>
            <img  src="/static/imghwm/default1.png"  data-src="showcode.php"  class="lazy"   onclick="this.setAttribute(&#39;src&#39;,&#39;showcode.php?&#39;+Math.random())" / alt="PHP生成图片验证码demo【OOP面向对象版本】" >
            <span>看不清?点击图片即可切换验证码</span><br/>
            <input type="submit" name="sub" value="登录/注册" />
        </form>
    </body>
</html>

2、createcode.class.php中的代码:

<?php
    /**
     * @Description  网站登录/注册验证码生成类
     * @Author  赵一鸣
     * @OnlineDemo http://www.zymseo.com/demo/verificationcode/code.html
     * @Date  2016年10月6日 
     */
    class Createcode{
        //画布资源
        public $img;
        //画布宽度
        private $img_width;
        //画布高度
        private $img_height;
        //画布颜色
        private $img_bgcolor;
        //验证码文字内容
        private $str_content;
        //生成的验证码内容
        private $code_content;
        //验证码颜色
        private $code_content_color;
        //构造函数
        public function __construct($img_width,$img_height,$str_content,$code_content_color){
            if($this->gdcheck()){
                $this->img_width = $img_width;
                $this->img_height = $img_height;
                $this->str_content = $str_content;
                $this->code_content_color = $code_content_color;
                $this->get_code();
                $this->session_code();
            }
        }
        //生成画布
        public function get_img(){
            //定义画布
            $this->img = imagecreatetruecolor($this->img_width, $this->img_height);
            //画布背景色
            $this->img_bgcolor = imagecolorallocate($this->img, mt_rand(0,255), mt_rand(0,255), mt_rand(0,255));
            //给画图填充背景色
            imagefill($this->img, 0, 0, $this->img_bgcolor);
            //取得画布的宽高
            $img_width = imagesx($this->img);
            $img_height = imagesy($this->img);
            //画布中插入验证码
            imagestring($this->img, 5, ($this->img_width/3), ($this->img_height/2.5), $this->code_content, imagecolorallocate($this->img, hexdec(substr($this->code_content_color, 1,2)), hexdec(substr($this->code_content_color, 3,2)), hexdec(substr($this->code_content_color, 5,2))));
            //画布中插入像素点
            $this->get_pix();
            //画布中插入直线
            $this->get_line();
            //画布显示
            header(&#39;Content-type:image/png&#39;);
            imagepng($this->img);
        }
        //生成验证码
        private function get_code(){
            $str_content_len = strlen($this->str_content);
            for($i=0;$i<4;$i++){
                $this->code_content .= substr($this->str_content, mt_rand(0,$str_content_len-1),1);
            }
        }
        //生成像素点
        private function get_pix(){
            for($j=0;$j<300;$j++){
                $image_pix .= imagesetpixel($this->img, mt_rand(0,$this->img_width), mt_rand(0,$this->img_height), imagecolorallocate($this->img, mt_rand(0,255), mt_rand(0,255), mt_rand(0,255)));
            }
            return $image_pix;
        }
        //生成直线
        private function get_line(){
            for($l=0;$l<2;$l++){
                $img_line .= imageline($this->img, mt_rand(0,$this->img_width), mt_rand(0,$this->img_height), mt_rand(0,$this->img_width), mt_rand(0,$this->img_height), imagecolorallocate($this->img, mt_rand(0,255), mt_rand(0,255), mt_rand(0,255)));
            }
            return $img_line;
        }
        //session存储验证码
        private function session_code(){
            session_start();
            $_SESSION[&#39;code&#39;] = $this->code_content;
        }
        //判断程序是否支持GD库
        private function gdcheck(){
            if(extension_loaded(&#39;gd&#39;)){
                return true;
            }else{
                return false;
                exit();
            }
        }
    }

3、checkcode.php中的代码:

<?php
/**
 * @Description  网站登录/注册验证码生成类
 * @Author  赵一鸣
 * @OnlineDemo http://www.zymseo.com/demo/verificationcode/code.html
 * @Date  2016年10月6日
 */
    header(&#39;Content-type:text/html;charset="utf-8"&#39;);
    session_start();
    if($_POST[&#39;code&#39;]!=&#39;&#39;){
        if($_SESSION[&#39;code&#39;]==$_POST[&#39;code&#39;]){
            echo &#39;<script type="text/javascript">
                    alert("验证码填写成功");
                    history.go(-1);
                </script>&#39;;
        }else{
            echo &#39;<script type="text/javascript">
                    alert("验证码填写失败");
                    history.go(-1);
                </script>&#39;;
        }
    }

4、showcode.php中的代码:

<?php
/**
 * @Description  网站登录/注册验证码生成类
 * @Author  赵一鸣
 * @OnlineDemo http://www.zymseo.com/demo/verificationcode/code.html
 * @Date  2016年10月6日
 */
    function __autoload($classname){
        include strtolower($classname).&#39;.class.php&#39;;
    }
    //定义验证码的取值范围
    $str_content = &#39;abcdefghijklmnopqrstuvwxyz0123456789&#39;;
    //验证码文字颜色
    $code_content_color = &#39;#ffffff&#39;;
    //初始化对象
    $code = new Createcode(100,30,$str_content,$code_content_color);
    $code->get_img();

原文地址:http://www.zymseo.com/php/334.html

转载请注明出处!


声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
3 周前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳图形设置
3 周前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您听不到任何人,如何修复音频
3 周前By尊渡假赌尊渡假赌尊渡假赌
WWE 2K25:如何解锁Myrise中的所有内容
3 周前By尊渡假赌尊渡假赌尊渡假赌

热工具

mPDF

mPDF

mPDF是一个PHP库,可以从UTF-8编码的HTML生成PDF文件。原作者Ian Back编写mPDF以从他的网站上“即时”输出PDF文件,并处理不同的语言。与原始脚本如HTML2FPDF相比,它的速度较慢,并且在使用Unicode字体时生成的文件较大,但支持CSS样式等,并进行了大量增强。支持几乎所有语言,包括RTL(阿拉伯语和希伯来语)和CJK(中日韩)。支持嵌套的块级元素(如P、DIV),

SublimeText3 Linux新版

SublimeText3 Linux新版

SublimeText3 Linux最新版

Dreamweaver Mac版

Dreamweaver Mac版

视觉化网页开发工具

SublimeText3 英文版

SublimeText3 英文版

推荐:为Win版本,支持代码提示!

DVWA

DVWA

Damn Vulnerable Web App (DVWA) 是一个PHP/MySQL的Web应用程序,非常容易受到攻击。它的主要目标是成为安全专业人员在合法环境中测试自己的技能和工具的辅助工具,帮助Web开发人员更好地理解保护Web应用程序的过程,并帮助教师/学生在课堂环境中教授/学习Web应用程序安全。DVWA的目标是通过简单直接的界面练习一些最常见的Web漏洞,难度各不相同。请注意,该软件中