This article mainly introduces a complete example of user registration, login and message in ThinkPHP, which is helpful to understand the operation process of ThinkPHP in detail. Friends in need can refer to it
This article describes the implementation of ThinkPHP in the form of examples, including users Registration, login, message and other functions. What needs to be noted here is thatis implemented using the D method when instantiating a user class when a user model exists.
UserActiion.class.php page:
create(); $result = $user->add(); if($result){ $this->assign("jumpUrl","__APP__/index/index"); $this->success('注册成功!'); }else{ //echo $user->getError(); $this->assign("jumpUrl","__APP__/user/register"); $this->error($user->getError()); } } public function register(){ $this->display(); } public function login(){ $this->display(); } public function checklogin(){ $username = $_POST['username']; $passwd = $_POST['passwd']; $user = D("user"); //$User->where('id=8')->find();这里的where 语句要注意一下,如果是其他字段的话后面一定要有单引号 $userinfo = $user->where("username ='$username'")->find(); if(!empty($userinfo)){ if($userinfo['passwd'] == $passwd){ Cookie::set('userid',$userinfo['id'],time()+3600*24); Cookie::set('username',$username,time()+3600*24); Cookie::set('lastlogintime',time(),time()+3600*24); $this->assign("jumpUrl","__APP__/index/index"); $this->success('登陆成功!'); }else{ $this->assign("jumpUrl","__APP__/user/login"); $this->error('密码出错,请重新输入!'); } }else{ $this->assign("jumpUrl","__APP__/user/login"); $this->error('用户名不存在!'); } } public function loginout(){ Cookie::delete('username'); Cookie::delete('lastlogintime'); $this->assign("jumpUrl","__APP__/index/index"); $this->success('您已经成功退出,欢迎您的下次登录!'); } }
IndexAction.class.php page:
create(); if(!$result){ $this->assign("jumpUrl","__URL__/index"); $this->error($content->getError());//如果创建失败,表示验证没有通过,输出错误信息 }else{//验证通过,进行其他操作 $content->userid=Cookie::get('userid'); $content->add(); $this->assign("jumpUrl","__URL__/index"); $this->success('添加成功!'); } } // 数据查询操作 public function index() { $content = new ContentModel(); $list = $content->findAll(); //用户的cookie $username = Cookie::get('username'); $lastlogintime = Cookie::get('lastlogintime'); $this->assign('list',$list); $this->assign('title','我的首页'); $this->assign('username',$username); $this->assign('lastlogintime',$lastlogintime); $this->display(); } // 删除操作 public function delete(){ $content = new ContentModel(); $id = $_GET['id']; if($content->where("id=$id")->delete()){ $this->assign("jumpUrl","__URL__/index"); $this->success('删除成功!'); }else{ $this->assign("jumpUrl","__URL__/index"); $this->error('删除失败!'); } } // 编辑操作 public function edit(){ $content = new ContentModel(); $id = $_GET['id']; if($id != '') { //$data = $content->select($id); $data = $content->where("id=$id")->select(); if(!empty($data)){ $this->assign('data',$data); }else{ echo "数据为空!"; } } $this->assign('title','编辑页面'); $this->display(); } // 更新操作 public function update(){ $content = new ContentModel(); //直接使用create(),自动会帮你进行数据的传值 /*$content->create(); $content->save(); // 根据条件保存修改的数据 echo "更新数据成功!";*/ // 使用post 传值过来,进行更新 $id = $_POST['id']; if($id != '') { $data['id'] = $id; $data['title'] = $_POST['title']; $data['content'] = $_POST['content']; if($content->save($data))// 根据条件保存修改的数据 { $this->assign("jumpUrl","__URL__/index"); $this->success('更新数据成功!'); } else{ $this->assign("jumpUrl","__URL__/index"); $this->success('更新数据失败!'); } }else { echo "保存数据失败!"; } } } ?>
ContentModel.class.php page:
UserModel.class.php page:
It should be noted here that when using automatic verification, instantiation You must use $user = D("user") instead of $user = M("user"). If you use M, an error will be reported. The D function is used to instantiate Model, and the M function instantiates a model without a model. document.
success.html page:
Related recommendations:
ThinkPHP implements simple login function
thinkphp implements 163, QQ mailbox method of sending and receiving emails_php skills
The above is the detailed content of ThinkPHP user registration login message complete example. For more information, please follow other related articles on the PHP Chinese website!