thinkphp自动验证和自动完成

tp验证码的自动验证小案例

模板文件

    <form action="" method="post">
        <p>
            User:
            <input type="text" name="name1" id="">
        </p>
        <p>
            PASS:
            <input type="password" name="pass1" id="">
        </p>
        <p>
            PASS:
            <input type="password" name="rpass1" id="">
        </p>
        <p>
            CODE
            <input type="text" name="code" id="">
            <img src="{:U('yzm')}" alt="">
        </p>
        <p>
            <input type="submit" value="提交">
        </p>
    </form>    

数据模型

<?php 

namespace Home\Model;

use  Think\Model;

class UserModel extends Model{
    // 字段映射
    protected $_map=array(
        "name1"=>"name",
        "pass1"=>"pass",

        );

    // 自动验证

    protected $_validate =array(
        // array(验证字段1,验证规则,错误提示,[验证条件,附加规则,验证时间]),、
        // 判断验证码是否存在
        array('code','require','请输入验证码'),
        // 判断验证码是否正确
        array('code','checks','验证码错误',1,'callback',1),
        // 判断用户名是否存在
        array('name','require','请输入用户名'),
        // 判断长度
        array('name','6,12','用户名长度必须6-12位',1,'length',1),
        // 判断用户名是否存在
        array('name','','用户名已经存在',1,'unique',1),
        // 密码是否正确
        array('pass','require','密码不存在'),
        // 密码长度
        array('pass','6,12','密码长度必须6-12位',1,'length',1),
        // 俩次密码是否一致
        array('pass','check_pwd','两次密码不一致',1,'callback',1),

        );

    // 检测验证码方法

    public function checks(){
        $verify = new \Think\Verify();
        return $verify->check($_POST['code'], $id="");
    }

    // 检测密码
    public function check_pwd(){
        if ($_POST['pass1']==$_POST['rpass1']) {
            # code...'
            return true;
        }else{
            return false;
        }
    }

    // 自动完成
    protected $_auto=array(
        // array(完成字段1,完成规则,[完成条件,附加规则]),
        // 给状态设置默认值
        array('statu','1',1),
        // 设置时间戳
        array('time','time',1,'function'),
        // 用户名大写
        array('name','asd',1,'callback'),

        );

    function asd(){
        return strtoupper($_POST['name1']);
    }
}





 ?>

控制器文件

<?php
namespace Home\Controller;
use Think\Controller;
class UserController extends Controller {
    public function index(){

        $model=M('student');

        // 多表查询
        // $sql="select student.name as sname,type.name tname from student,type where student.cid=type.id";

        // 发动sql语句
        // $data=$model->query($sql);

        $data=$model->field('student.name sname,type.name tname')->join("type on type.id=student.cid")->select();

        echo $model->getlastSql();
        echo "<pre>";
        print_r($data);
        echo "</pre>";

    }

    public function index1(){
        $model=M('student');

        $sql="select  student.name as sname,type.name tname from student left join type on student.cid=type.id";
           $data=$model->query($sql);

           $data=$model->field('student.name sname,type.name tname')->join('type on type.id=student.cid','right')->select();
           echo $model->getLastSql();
           echo "<pre>";
           print_r($data);
           echo "</pre>";
    }

    // 添加页面


    public function add(){

        if (IS_POST) {
            $model=D('user');
        
            if ($model->create()) {
                # code...

                if ($model->add()) {
                    # code...
                }

            }else{
                echo $model->getError();
            }

        }else{
            $this->display();
        }
    }

    public function add(){
        if(){
            
        }else{
            
        }
    }


    public function yzm(){
        // $Verify = new \Think\Verify();

        // // // 设置字体大小
        // // $Verify->fontSize=30;

        // // // 使用背景图像
        // // $Verify->useImgBg=true;

        // // // 是否干扰线
        // // $Verify->useCurve=false;

        // // // 是否使用小点
        // // $Verify->useNoise=true;

        // // // 验证码宽度
        // // $Verify->imageW=500;

        // // // 验证码高度
        // // $Verify->imageH=200;

        // // // 验证码位数
        // // $Verify->length=3;

        // // // 设置干扰颜色
        // // $Verify->bg=array(255,0,0);

        // // 设置验证码字符集合
        // // $Verify->codeSet="1234567890";

        // // 设置字体
        // $Verify->fontttf="STXINWEI.TTF";

        // $Verify->zhSet="我爱你中国";

        // // 使用中文
        // $Verify->useZh=true;
        // $Verify->entry();

        $Verify =     new \Think\Verify();
        // 验证码字体使用 ThinkPHP/Library/Think/Verify/ttfs/5.ttf
        $Verify->fontttf="STXINWEI.TTF";
        $Verify->zhSet="我爱你中国";
        $Verify->length=1;
        $Verify->imageW=100;
        $Verify->useZh = true; 
        $Verify->entry();
    }

    function check_verify($code, $id = ''){
        $verify = new \Think\Verify();
        return $verify->check($code, $id);
    }
public function yzm1(){ if (IS_POST) { # code... echo "<pre>"; print_r($_SESSION); echo "</pre>"; if ($this->check_verify($_POST['code'])) { echo "1"; }else{ echo "2"; } }else{ $this->display(); } } }

 

posted @ 2017-09-14 20:28  Howie_Tang  阅读(486)  评论(0编辑  收藏  举报