TP框架 商城前台用户注册方法

//注册
public function register(){
if(IS_POST){
//接收数据
$data = I('post.');
$model = D('User');
$data['username'] = $data['email'] ? $data['email'] : $data['phone'];
//使用模型的create方法自动创建数据集,会自动实现自动验证等功能
if(!$model -> create($data)){
//检测失败
$error = $model -> getError();
$this -> error($error);
}
$res = $model -> add();
if($res){
//注册成功
$this -> success("注册成功", U("Home/User/login"));
}else{
$this -> errro('注册失败');
}
}else{
//临时关闭模板布局
layout(false);
$this -> display();
}

}

//登录
public function login(){
if(IS_POST){
//接收数据
$data = I('post.');
//字段检测
if(!$data['username'] || !$data['password']){
$this -> error('用户名和密码不能为空');
}
$username = $data['username'];
//根据用户名查询数据库
$user = M('User') -> where("username = '{$username}' or phone='{$username}' or email='{$username}'") -> find();
//验证密码
if($user && $user['password'] == encrypt_password($data['password'])){
//登录成功
$this -> success('登录成功', $back_url);
}else{
$this -> error('登录失败');
}
}else{
//临时关闭模板布局
layout(false);
//优化,判断是否已登录,已登录则跳转到首页
if(session('?user_info')){
$this -> redirect('Home/Index/index');
}
$this -> display();
}

}

//退出
public function logout(){
session(null);
$this -> redirect('Home/Index/index');
}
}

原文地址:https://www.cnblogs.com/songyanan/p/7425905.html