您的位置:首页 > 编程语言 > PHP开发

thinkphp注册功能---图片验证码

2015-11-17 11:48 609 查看
一、控制器类文件

<?php

namespace Home\Controller;

use Think\Controller;

class RegisterController extends Controller {
// html模板渲染
public function Register(){
$this->display();
}

// 普通用户注册
public function insert(){
$User = D('User');
if($User->create($_POST, 1)) {    // 注意此处,括号里的参数要填上,否则就会默认,自动验证可能不起作用;
$verify = I('post.reg_code','');  
if(!check_verify($verify)){
$this->error("亲,验证码输错了哦!",$this->site_url,9); 
}
else{
$result = $User->add();
           if($result) {
               $this->success('数据添加成功!');
           }else{
               $this->error('数据添加错误!');
           }
}

        }else{

            $this->error($User->getError());

        }   
}

// 生成验证码
Public function verify(){
// 其它版本
// import('ORG.Util.Image');
// Image::buildImageVerify();

// 3.2版本
$Verify = new \Think\Verify();  
$Verify->fontSize = 500;
$Verify->length = 4;
$Verify->useNoise = true;

    $Verify->entry();  
}

}

二、model层文件

<?php

namespace Home\Model;

use Think\Model;

class UserModel extends Model {

    // 定义自动验证

    protected $_validate = array(

        array('user_name','require','用户名必须'),

        array('user_name', '','用户名已经存在', 0 , 'unique', self::MODEL_INSERT),  

        array('user_passwd','require','密码必填'),  

        array('reuser_passwd','require','重复密码必填'),  

        array('user_passwd','reuser_passwd','两次密码不一致',0,'confirm'),

        array('reg_code', 'require', '您没有填写验证码'),

        // array('reg_code', ),

    );

    // 定义自动完成

    protected $_auto    =   array(

        array('user_passwd','md5',1,'function'),

        array('create_time','time',1,'function'),

    );

 }

三、html文件

<div class="register_box">
<h3>用户注册</h3>
<form method="post" action="__URL__/insert">
<input type="text" name="user_name" placeholder="输入您的账号" />
<input type="text" name="user_passwd" placeholder="输入您的密码" />
<input type="text" name="reuser_passwd" placeholder="再次输入您的密码" />
<input class="reg_code" name="reg_code" type="text" placeholder="输入验证码" />
<img src="__URL__/verify" width="80px" height="30px" onclick="this.src='http://localhost/index.php/Home/Register/verify'"/>
<input class="register_btn" type="submit" name="" value="注册" />
<p>已有账号,
<a href="__APP__/Home/Login/login">立即登录</a>
</p>
</form>
</div>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: