thinkphp实现用户注册_thinkphp实现用户注册的例子

更新时间:2019-11-08    来源:thinkphp    手机版     字体:

【www.bbyears.com--thinkphp】

通过一个表单实现提交并验证数据最终写入数据库的过程

先看表结构

表名是user
字段:
id name pass mail ifadmin createtime ip
 
看我的模板表单:


姓名:

密码:word" name="pass">

重复密码:word" name="repass">

邮箱:


 


<script>
function fleshVerify(){
    //重载验证码
    var timenow = new Date().getTime();
    document.getElementById("verifyImg").src= "__APP__/Yzm/verify/"+timenow;
    }
</script>

看我的IndexAction.class.php文件

public function index()
{
    
            
    //dump($list);
    
    //$a = C("DB_HOST");
 $this->assign("aa",$list);
 $this->assign("page",$show);
 $this->assign("title","注册页面!");
 $this->display();
}
 
public function insert()
{
    if($_SESSION["verify"]!=md5($_POST["verify"])){
        
        $this->error("验证码错误!!");        
    }
 
    load("extend");
    
    $data =D(User);    
        
    if($data->create()){
        
        
        //$data->ip = get_client_ip();
        //$data->createtime=time();
        if(false !==$data->add()) {
            $this->success("数据添加成功!");
        }else{
            $this->error("数据写入错误");
        }
        
        
    }else{
        header("Content-Type:text/html; charset=utf-8");
        echo($data->getError());
    
    }
    
            
}

就是两个方法,一个是index默认方法,一个是提交到insert方法。。
还有一个model类

还有一个验证码的就不贴出来了。

UserModel.class.php

class UserModel extends Model {
    
    protected $_validate = array(
    array("name","require","姓名必填!"),
    array("pass","require","密码必填!"),
    //array( "repass","pass","确认密码不正确 ", 0 , ’confirm’ ),
    array("mail","require","邮箱必填!"),
    array("mail","email","邮箱格式错误!",2), 
    array("name","","姓名已存在!",0,"unique",self::MODEL_INSERT),       
    );
protected $_auto     =     array(
        array( "pass","md5",3,"function") ,
        array("ifadmin","0",self::MODEL_INSERT),
        array( "ip","get_client_ip",3,"function") ,
        array( "createtime","time",3,"function") ,
           );
 
    
           
           
           
 //protected $trueTableName = "top_user";
 
 
 
}
 
?>

Model类主要是完成自动检测和自动填充功能。。

本文来源:http://www.bbyears.com/jiaocheng/78096.html