ThinkPHP user registration login message complete example

不言
Release: 2023-03-30 07:22:02
Original
1410 people have browsed it

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 that is 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('您已经成功退出,欢迎您的下次登录!');
}
}
Copy after login

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 "保存数据失败!";
}
}
}
?>
Copy after login

ContentModel.class.php page:

Copy after login

UserModel.class.php page:

Copy after login


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:






信息提示

信息提示

{$message}
2秒后返回指定页面!
如果浏览器无法跳转,请点击此处

Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact [email protected]
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!