zoukankan      html  css  js  c++  java
  • 19)加了验证码验证

    首先是目录展示:

      

    改动部分代码展示:

        Captcha.class.php

          

     1 <?php
     2 
     3 
     4 /**
     5  * 验证码 工具
     6  */
     7 class Captcha {
     8 
     9     /**
    10      * 输出生成的验证码输出
    11      *
    12      * @param $code_len=4 码值的长度
    13      * @return void
    14      */
    15     public function generate($code_len=4) {
    16         //生成码值
    17         $chars = 'ABCDEFGHIJKLMNPQRSTUVWXYZ123456789';//所有可能字符
    18         $chars_len = strlen($chars);
    19         $code = '';//初始化码值字符串
    20         for($i=1; $i<=$code_len;++$i) {
    21             $rand_index = mt_rand(0, $chars_len-1);
    22             $code .= $chars[$rand_index];//字符串支持[]操作,通过下标取得某个字符
    23         }
    24         //echo $code;
    25 
    26         //存储于session,用于验证
    27         @session_start();//保证session机制一定是开启的,同时重复开启不会报错,@屏蔽错误。
    28         $_SESSION['captcha_code'] = $code;
    29 
    30         //生成验证码图片
    31 
    32         //背景图
    33         $bg_file = TOOL_PATH . 'captcha/captcha' . mt_rand(1, 2) . '.png';
    34 
    35         //基于jpg格式的图片创建画布
    36         $img = imagecreatefrompng($bg_file);
    37 
    38         //随机分配字符串颜色
    39         $str_color = mt_rand(1, 3) == 1 ? imageColorAllocate($img, 0, 0, 0) : imageColorAllocate($img, 0xff, 0xff, 0xff);
    40 
    41         //字符串
    42         $font = 5;
    43         // 画布尺寸
    44         $img_w = imageSX($img);
    45         $img_h = imageSY($img);
    46         // 字体的尺寸
    47         $font_w = imageFontWidth($font);
    48         $font_h = imageFontHeight($font);
    49         // 字符串的尺寸
    50         $code_w = $font_w * $code_len;
    51         $code_h = $font_h;
    52         $x = ($img_w-$code_w)/2;
    53         $y = ($img_h-$code_h)/2;
    54         imageString($img, $font, $x, $y, $code, $str_color);
    55 
    56         //输出
    57         header('Content-Type: image/jpeg;');
    58         imagepng($img);
    59 
    60         //
    61         imageDestroy($img);
    62     }
    63 
    64     /**
    65      * 验证
    66      * @param $request_code 用户表单中提交的码值
    67      * @return bool 是否匹配
    68      */
    69    
    70     public function checkCaptcha($request_code) {
    71         @session_start();
    72         //严格点,存在且相等(不区分大小写)。
    73         //strCaseCmp()不区分大小写字符串比较,返回值负,第一个小,返回正,第一个大,返回0,相等。strCmp也是类似,不过是区分大小写比较。
    74         $result = isset($request_code) && isset($_SESSION['captcha_code']) && (strCaseCmp($request_code, $_SESSION['captcha_code'])==0);
    75 
    76         //安全考虑,销毁session中的验证码值
    77         unset($_SESSION['captcha_code']);
    78 
    79         return $result;
    80 
    81     }
    82    
    83 }

           

    AdminC.controller.class.php(还有后面的captcha方法,也是新加的,我忘了画了)

        

     1 <?php
     2     /**
     3      * Created by PhpStorm.
     4      * User: Interact
     5      * Date: 2017/8/20
     6      * Time: 14:22
     7      */
     8    
     9 class AdminC extends  Controller {
    10    
    11     
    12     public  function  login(){
    13 //        require
    14         require APPLICATION_PATH.'back/view/login.html';
    15     }
    16     /**
    17      * 验证管理员是否合法
    18      */
    19     public function check() {
    20 //       echo "MC天佑MC天佑MC天佑";
    21 //        echo $_REQUEST['username'];
    22         // 获得表单数据
    23         /*echo $_REQUEST['username'];
    24         echo '
    ';
    25         echo $_REQUEST['password'];*/
    26         $temp='';
    27        $admin_name = $_REQUEST['username'];
    28         $admin_pass = $_REQUEST['password'];
    29         $admin_name=addslashes($admin_name);
    30         $admin_pass=addslashes($admin_pass);
    31         $admin_captcha=$_REQUEST['captcha'];
    32         //从数据库中验证管理员信息是否存在合法
    33         $m_admin = Factory::M('AdminModel');
    34         $Captcha=new Captcha();
    35         
    36        if(!$Captcha->checkCaptcha($admin_captcha)){
    37             
    38             $this->_jump('index.php?p=back&c=AdminC&a=login','验证码错误',3);
    39         }else{
    40             $temp='yes';
    41         }
    42      
    43         if (($m_admin->check($admin_name, $admin_pass))&&($temp==='yes')) {
    44 //            //验证通过,合法
    45 //            echo '合法,直接跳转到后台首页';
    46            session_start();
    47             $_SESSION['is_login']='yes';
    48             //new SessionDB();
    49             $this->_jump('index.php?p=back&c=BACkC&a=index');
    50         } else {
    51             // 非法
    52 //            echo '非法, 提示,跳转到后台登陆页面index.php?p=back&c=Admin&a=login';
    53             $this->_jump('index.php?p=back&c=AdminC&a=login','用户名或密码错误');
    54         }
    55 //
    56     }
    57     public  function captcha(){
    58         //利用Captcha工具类
    59         $t_captcha=new Captcha();
    60         $t_captcha->generate();
    61     }
    62 }

    login.html代码:

          thinkphp5中这样写的

            <img src="{:captcha_src()}" alt="captcha" onclick="this.src='{:captcha_src()}'"  />

      

     1 <html xmlns="http://www.w3.org/1999/xhtml"><head>
     2 <title>ECSHOP 管理中心</title>
     3 <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
     4 <link type="text/css" rel="stylesheet" href="web/back/styles/general.css">
     5 <link type="text/css" rel="stylesheet" href="web/back/styles/main.css">
     6 
     7 <style type="text/css">
     8 body {
     9   color: white;
    10 }
    11 </style>
    12 </head>
    13 <body style="background: #278296">
    14 <form onsubmit="" name="theForm" action="index.php?p=back&c=AdminC&a=check" method="post">
    15   <table cellspacing="0" cellpadding="0" align="center" style="margin-top: 100px">
    16   <tbody><tr>
    17     <td><img border="0" width="178" height="256" alt="ECSHOP" src="web/back/images/login.png"></td>
    18     <td style="padding-left: 50px">
    19       <table>
    20       <tbody><tr>
    21         <td>管理员姓名:</td>
    22         <td>
    23           <label>
    24             <input type="text" name="username" id="username">
    25           </label>
    26           </td>
    27       </tr>
    28       <tr>
    29         <td>管理员密码:</td>
    30         <td>
    31           <label>
    32             <input type="password" name="password">
    33           </label>
    34           </td>
    35       </tr>
    36 
    37       <tr>
    38         <td>验证码:</td>
    39         <td><input type="text" class="capital" name="captcha"></td>
    40       </tr>
    41       <tr>
    42       <td align="right" colspan="2">
    43         <img border="1" width='150' height='30' title="看不清?点击更换另一个验证码。" style="cursor: pointer;" onclick="this.src='index.php?p=back&c=AdminC&a=captcha'" alt="CAPTCHA"
    44              src="index.php?p=back&c=AdminC&a=captcha">
    45       </td>
    46       </tr>
    47 
    48       <tr><td colspan="2"><input type="checkbox" id="remember" name="remember" value="1"><label for="remember">请保存我这次的登录信息。</label></td></tr>
    49       <tr><td>&nbsp;</td><td><input type="submit" class="button" value="进入管理中心"></td></tr>
    50       <tr>
    51         <td align="right" colspan="2">» <a style="color:white" href="index.php?p=front">返回首页</a> » <a style="color:white" href="index.php?p=back&c=AdminC&a=forget_pwd">您忘记了密码吗?</a></td>
    52       </tr>
    53       </tbody></table>
    54     </td>
    55   </tr>
    56   </tbody></table>
    57 </form>
    58 </body></html>
  • 相关阅读:
    golang学习笔记---flag包
    golang学习笔记 --- 结构体(struct)
    golang学习笔记---映射(map)
    golang学习笔记----slice(22)
    golang学习笔记---数组(22)
    golang学习笔记 ---数据类型转换(strconv包)
    golang学习笔记 ---slice(2)
    golang学习笔记 ---slice
    golang学习笔记--中英文字符串截取
    golang学习笔记 ---如何将interface转为int, string, slice, struct等类型
  • 原文地址:https://www.cnblogs.com/xiaoyoucai/p/7407398.html
Copyright © 2011-2022 走看看