13.2.2 验证码类的编写1

13.2.2 验证码类的编写1

code.php

<?php
    //开启session
    session_start();
    include "vcode.class.php";
    //构造方法
    $vcode = new Vcode(80, 25, 4);
    //将验证码放到服务器自己的空间保存一份
    $_SESSION = $vcode->getcode();
    //将验证码图片输出
    $vcode->outimg();

reg.php

<body>
    <form action="reg.php" method="post">
        username: <input type="text" name="username"> <br>
        password: <input type="password" name="password"> <br>
        code: <input type="text" size="4" name="code"> 
              <img src="code.php" />  <br>

        <input type="submit" name="dosubmit" value="登 录"> <br>
        
    </form>
</body>

vcode.class.php

<?php
    class Vcode {
        private $width;    //宽
        private $height;   //高
        private $num;      //数量
        private $code;    //验证码


        //构造方法, 三个参数
        function __construct($width, $height, $num) {
            $this->width = $width;
            $this->height = $height;
            $this->num = $num;
            $this->code = $this->createcode(); //调用自己的方法
        }



        //获取字符的验证码, 用于保存在服务器中
        function getcode() {
            return $this->code;
        }

                

        //输出图像
        function outimg() {
            //创建背景 (颜色, 大小, 边框)
            $this->createback();

            //画字 (大小, 字体颜色)


            //干扰元素(点, 线条)
            

            //输出图像
        }

        //创建背景
        private function createback() {
        
        }

        //画字
        private function outstring() {
        
        }

        //设置干扰元素
        private function setdisturbcolor() {
        
        }

        //输出图像
        private function printimg() {
            
        }

        //生成验证码字符串
        private function createcode() {
            $codes = "3456789abcdefghijkmnpqrstuvwxyABCDEFGHIJKLMNPQRSTUVWXY";

            $code = "";

            for($i=0; $i < $this->num; $i++) {
                $code .=$codes{rand(0, strlen($codes)-1)};  
            }

            return $code;
        }

    }
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,131评论 19 139
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 174,081评论 25 709
  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 31,805评论 18 399
  • 编者按:承接上期,在富爸爸教授理财知识的过程中,对本书作者(罗伯特·清崎),说了些什么?有哪些话能带给我们一些启发...
    梁读阅读 1,113评论 0 1
  • 我知道父亲也有故事,只是他不愿意说。 那天早上,走在去驾校的路上,北方的天气,出奇的冷,车里开着暖气,也觉得有些冰...
    老tan子阅读 201评论 0 1