Home Backend Development PHP Tutorial ThinkPHP user registration login message complete example

ThinkPHP user registration login message complete example

Jun 06, 2018 pm 02:15 PM
thinkphp Example User registration message Log in

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(&#39;注册成功!&#39;);
}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[&#39;username&#39;];
$passwd = $_POST[&#39;passwd&#39;];
$user = D("user");
//$User->where(&#39;id=8&#39;)->find();这里的where 语句要注意一下,如果是其他字段的话后面一定要有单引号
$userinfo = $user->where("username =&#39;$username&#39;")->find();
if(!empty($userinfo)){
if($userinfo[&#39;passwd&#39;] == $passwd){
Cookie::set(&#39;userid&#39;,$userinfo[&#39;id&#39;],time()+3600*24);
Cookie::set(&#39;username&#39;,$username,time()+3600*24);
Cookie::set(&#39;lastlogintime&#39;,time(),time()+3600*24);
$this->assign("jumpUrl","__APP__/index/index");
$this->success(&#39;登陆成功!&#39;);
}else{
$this->assign("jumpUrl","__APP__/user/login");
$this->error(&#39;密码出错,请重新输入!&#39;);
}
}else{
$this->assign("jumpUrl","__APP__/user/login");
$this->error(&#39;用户名不存在!&#39;);
}
}
public function loginout(){
Cookie::delete(&#39;username&#39;);
Cookie::delete(&#39;lastlogintime&#39;);
$this->assign("jumpUrl","__APP__/index/index");
$this->success(&#39;您已经成功退出,欢迎您的下次登录!&#39;);
}
}
Copy after login

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(&#39;userid&#39;);
$content->add();
$this->assign("jumpUrl","__URL__/index");
$this->success(&#39;添加成功!&#39;);
}
} 
// 数据查询操作  
public function index() {
$content = new ContentModel();
$list = $content->findAll();  
//用户的cookie
$username = Cookie::get(&#39;username&#39;);
$lastlogintime = Cookie::get(&#39;lastlogintime&#39;);
$this->assign(&#39;list&#39;,$list);    
$this->assign(&#39;title&#39;,&#39;我的首页&#39;);
$this->assign(&#39;username&#39;,$username);
$this->assign(&#39;lastlogintime&#39;,$lastlogintime);
$this->display();  
} 
// 删除操作
public function delete(){
$content = new ContentModel();
$id = $_GET[&#39;id&#39;];
if($content->where("id=$id")->delete()){
$this->assign("jumpUrl","__URL__/index");
$this->success(&#39;删除成功!&#39;);
}else{
$this->assign("jumpUrl","__URL__/index");
$this->error(&#39;删除失败!&#39;);
}
} 
// 编辑操作
public function edit(){
$content = new ContentModel();
$id = $_GET[&#39;id&#39;];
if($id != &#39;&#39;)
{
//$data = $content->select($id);
$data = $content->where("id=$id")->select();
if(!empty($data)){
$this->assign(&#39;data&#39;,$data);
}else{
echo "数据为空!";
}
}
$this->assign(&#39;title&#39;,&#39;编辑页面&#39;);
$this->display();
}
// 更新操作
public function update(){
$content = new ContentModel();
//直接使用create(),自动会帮你进行数据的传值
/*$content->create();
$content->save(); // 根据条件保存修改的数据
echo "更新数据成功!";*/
// 使用post 传值过来,进行更新
$id = $_POST[&#39;id&#39;];
if($id != &#39;&#39;)
{
$data[&#39;id&#39;] = $id;
$data[&#39;title&#39;] = $_POST[&#39;title&#39;];
$data[&#39;content&#39;] = $_POST[&#39;content&#39;];
if($content->save($data))// 根据条件保存修改的数据
{
$this->assign("jumpUrl","__URL__/index");
$this->success(&#39;更新数据成功!&#39;);
}
else{
$this->assign("jumpUrl","__URL__/index");
$this->success(&#39;更新数据失败!&#39;);
}
}else
{
echo "保存数据失败!";
}
}
}
?>
Copy after login

ContentModel.class.php page:

<?php
class ContentModel extends Model{
/*
* 自动验证
* array(验证字段,验证规则,错误提示,验证条件,附加规则,验证时间)
*/ 
protected $_validate = array(
array(&#39;title&#39;,&#39;require&#39;,&#39;标题必须填写!&#39;),
array(&#39;content&#39;,&#39;require&#39;,&#39;内容必须填写!&#39;), 
);
/* 
* 自动填充
* array(填充字段,填充内容,填充条件,附加规则)
*/
protected $_auto = array(
array(&#39;addtime&#39;,&#39;time&#39;,1,&#39;function&#39;),
);
}
?>
Copy after login

UserModel.class.php page:

<?php
class UserModel extends Model{
protected $_validate = array(
array(&#39;username&#39;,&#39;&#39;,&#39;帐号名称已经存在!&#39;,0,&#39;unique&#39;,1), 
);  
}
?>
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:

<!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=&#39;{$jumpUrl}&#39;" />
<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>
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!

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 admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Why can't I register at the Bitget Wallet exchange? Why can't I register at the Bitget Wallet exchange? Sep 06, 2024 pm 03:34 PM

There are various reasons for being unable to register for the BitgetWallet exchange, including account restrictions, unsupported regions, network issues, system maintenance and technical failures. To register for the BitgetWallet exchange, please visit the official website, fill in the information, agree to the terms, complete registration and verify your identity.

Why does Douyin have two accounts? How to install two TikToks on your mobile phone? Why does Douyin have two accounts? How to install two TikToks on your mobile phone? May 06, 2024 pm 09:28 PM

In the digital age, social media has become an integral part of people's lives. Douyin, as one of the most popular short video platforms in China, has attracted a large number of users. Some users even registered two accounts. So, why does Douyin have two accounts? This article will answer this question for you and explain how to install two Douyin accounts on your phone. 1. Why does Douyin have two accounts? Functional differentiation: Some users will differentiate accounts based on content type or function. For example, one account is used to share daily life, and another account is used to demonstrate professional skills. 2. Privacy protection: Some users hope to protect their privacy through two accounts, separate life and work, and avoid information leakage. 3. Interaction needs: Some users may register two due to interaction needs

DeepSeek official website entrance and latest promotional activities DeepSeek official website entrance and latest promotional activities Feb 19, 2025 pm 05:15 PM

DeepSeek's official website is now launching multiple discount activities to provide users with a shopping experience. New users sign up to get a $10 coupon, and enjoy a 15% limited time discount for the entire audience. Recommend friends can also earn rewards, and you can accumulate points for redemption of gifts when shopping. The event deadlines are different. For details, please visit the DeepSeek official website for inquiries.

ThinkPHP6 routing: How to completely obtain URL parameters containing special characters such as Chinese? ThinkPHP6 routing: How to completely obtain URL parameters containing special characters such as Chinese? Apr 01, 2025 pm 02:51 PM

ThinkPHP6 routing parameters are processed in Chinese and complete acquisition. In the ThinkPHP6 framework, URL parameters containing special characters (such as Chinese and punctuation marks) are often processed...

How to register XT.COM exchange account in mainland China? How to register XT.COM exchange account in mainland China? Aug 16, 2024 pm 06:51 PM

Mainland users can register on the XT.COM exchange through the following steps: Visit the XT.COM official website. Click the "Register" button in the upper right corner. Select the "Mobile Registration" option. Enter your mainland mobile phone number, obtain and enter the verification code. Set a password. Complete authentication. Registration completed.

How to solve the problem of cURL error 77 when connecting to Elasticsearch 8 using ThinkPHP6 and elasticsearch-php clients? How to solve the problem of cURL error 77 when connecting to Elasticsearch 8 using ThinkPHP6 and elasticsearch-php clients? Mar 31, 2025 pm 11:36 PM

Using the ThinkPHP6 framework combined with elasticsearch-php client to operate Elasticsearch...

Sesame Open Door Official Website Trading Platform Sesame Open Door Official Website Exchange Registration Entrance Sesame Open Door Official Website Trading Platform Sesame Open Door Official Website Exchange Registration Entrance Feb 28, 2025 am 10:57 AM

Gate.io Sesame Open is the world's leading blockchain digital asset trading platform, including fiat currency trading, currency trading, leveraged trading, perpetual contracts, ETF leveraged tokens, wealth management, Startup initial public offering and other sections, providing users with security, stability, openness and transparency.

How to change Douyin recommendations to the initial state? What is the impact of the recommendation becoming the initial state? How to change Douyin recommendations to the initial state? What is the impact of the recommendation becoming the initial state? May 06, 2024 pm 02:10 PM

As a national short video platform, Douyin’s recommendation algorithm has always attracted much attention from users. In daily use, we may encounter situations where Douyin recommendations become no longer accurate. So, how do Douyin recommendations change to the initial state? 1. How do Douyin recommendations change to the initial state? Douyin’s recommendation algorithm is mainly optimized based on users’ behavioral data, including viewing time, likes, comments, shares, etc. When these data change significantly, the recommendation algorithm may think that the user's interests have changed and therefore adjust the recommended content. The following situations may cause Douyin recommendations to change to the initial state: users have been inactive for a long time, are not interested in new content, or have changed their viewing habits, etc. New user registration: For new users, the recommendation system will recommend popular and popular videos by default to help

See all articles