tp验证码

一、生成验证码  

1、commonController.class.php

   class CommonController extends Controller{

  public function getVerify(){

     $Verify = new \Think\Verify();

     $Verify->entry();

}

public function validatVerify(){

 二、验证码检测

   $verify = new \Think\Verify();

    return $verify->check($code, $id); //$id是多个验证的情况下写的,一个不需要

}

}

三、地址输入就能看了

四、视图

<img src="{:U("Common/get....")}"  onclick='src={:U("Common/get....")}' style="cursor=pointer"/>

 

indexcontroller.class.php  //用于验证的

public function showSession(){

  dump($_SESSION);

  $verify = new \Think\Verify();

  return $verify->check($code, $id);

}

===========

控制器-》前置验证  _before_insert(){}

=========================以下自己写的

====================================

 start

一、在里面写前置方法LoginController.class.php (验证传过来的值是否正确)

 

    public function _before_login(){
        $verify = new \Think\Verify();
        if(!$verify->check(I("post.verify_code"))){
            //$this->redirect("login");
            $this->error("提交失败");
            //exit();
        }
    }

 

 二、可以单独建立一个获取验证码的控制器

 VerifyController.class.php

<?php
namespace Admin\Controller;
header('Content-type:text/html;charset=utf-8');
use Think\Controller;
class VerifyController extends Controller {
    public function getVerify(){
        $Verify = new \Think\Verify();
$Verify->fontSize = 50; $Verify->length = 4; //$Verify->useImgBg = true; $Verify->useNoise = false; $Verify->bg = [255, 255, 128];
$Verify->entry(); } }

三、在视图html页面

            <div class="input-group">
                <span class="input-group-addon"><i class="fa fa-reddit"></i></span>
                <input type="text" class="form-control" placeholder="verify code" name="verify_code" style="width:130px">&nbsp;&nbsp;
              //在方法中获取验证码
<img src="{:U('Verify/getVerify')}" style="width:110px;height:40px;cursor: pointer;" onclick="src='{:U('Verify/getVerify')}'"/> </div>

 

posted @ 2015-08-12 11:33  __Rain丶  阅读(350)  评论(0编辑  收藏  举报