ThinkPHP user registration login message complete example
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:
<?php class UserAction extends Action{ public function add(){ $user = D("user"); $user->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:
<?php // 本类由系统自动生成,仅供测试用途 class IndexAction extends Action{ public function insert() { $content = new ContentModel(); $result = $content->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:
<?php class ContentModel extends Model{ /* * 自动验证 * array(验证字段,验证规则,错误提示,验证条件,附加规则,验证时间) */ protected $_validate = array( array('title','require','标题必须填写!'), array('content','require','内容必须填写!'), ); /* * 自动填充 * array(填充字段,填充内容,填充条件,附加规则) */ protected $_auto = array( array('addtime','time',1,'function'), ); } ?>
UserModel.class.php page:
<?php class UserModel extends Model{ protected $_validate = array( array('username','','帐号名称已经存在!',0,'unique',1), ); } ?>
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:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <meta http-equiv="refresh" content="20; url='{$jumpUrl}'" /> <title>信息提示</title> </head> <body> <p id="man_zone"> <table width="40%" border="1" align="center" cellpadding="3" cellspacing="0" class="table" style="margin-top:100px;"> <tr> <th align="center" style="background:#cef">信息提示</th> </tr> <tr> <td><p>{$message}<br /> 2秒后返回指定页面!<br /> 如果浏览器无法跳转,<a href="{$jumpUrl}" rel="external nofollow" >请点击此处</a>。</p></td> </tr> </table> </p> </body> </html>
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!

Reasons for PHPSession failure include configuration errors, cookie issues, and session expiration. 1. Configuration error: Check and set the correct session.save_path. 2.Cookie problem: Make sure the cookie is set correctly. 3.Session expires: Adjust session.gc_maxlifetime value to extend session time.

Methods to debug session problems in PHP include: 1. Check whether the session is started correctly; 2. Verify the delivery of the session ID; 3. Check the storage and reading of session data; 4. Check the server configuration. By outputting session ID and data, viewing session file content, etc., you can effectively diagnose and solve session-related problems.

Multiple calls to session_start() will result in warning messages and possible data overwrites. 1) PHP will issue a warning, prompting that the session has been started. 2) It may cause unexpected overwriting of session data. 3) Use session_status() to check the session status to avoid repeated calls.

Configuring the session lifecycle in PHP can be achieved by setting session.gc_maxlifetime and session.cookie_lifetime. 1) session.gc_maxlifetime controls the survival time of server-side session data, 2) session.cookie_lifetime controls the life cycle of client cookies. When set to 0, the cookie expires when the browser is closed.

The main advantages of using database storage sessions include persistence, scalability, and security. 1. Persistence: Even if the server restarts, the session data can remain unchanged. 2. Scalability: Applicable to distributed systems, ensuring that session data is synchronized between multiple servers. 3. Security: The database provides encrypted storage to protect sensitive information.

Implementing custom session processing in PHP can be done by implementing the SessionHandlerInterface interface. The specific steps include: 1) Creating a class that implements SessionHandlerInterface, such as CustomSessionHandler; 2) Rewriting methods in the interface (such as open, close, read, write, destroy, gc) to define the life cycle and storage method of session data; 3) Register a custom session processor in a PHP script and start the session. This allows data to be stored in media such as MySQL and Redis to improve performance, security and scalability.

SessionID is a mechanism used in web applications to track user session status. 1. It is a randomly generated string used to maintain user's identity information during multiple interactions between the user and the server. 2. The server generates and sends it to the client through cookies or URL parameters to help identify and associate these requests in multiple requests of the user. 3. Generation usually uses random algorithms to ensure uniqueness and unpredictability. 4. In actual development, in-memory databases such as Redis can be used to store session data to improve performance and security.

Managing sessions in stateless environments such as APIs can be achieved by using JWT or cookies. 1. JWT is suitable for statelessness and scalability, but it is large in size when it comes to big data. 2.Cookies are more traditional and easy to implement, but they need to be configured with caution to ensure security.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 English version
Recommended: Win version, supports code prompts!

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

Atom editor mac version download
The most popular open source editor
