How to implement the login function under the thinkphp framework

不言
Release: 2023-04-04 08:38:02
forward
5854 people have browsed it

The content of this article is about the implementation method of the login function under the thinkphp framework. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

The login function is a common function in PHP programming. The ThinkPHP example in this article mainly enters the homepage after successful registration and tells you the function of logged in user. The specific implementation steps are as follows:

Step 1: Add:

## to the config.php file #The complete implementation code is as follows:

public function insert()
{
header('Content-Type:text/html; charset=utf-8');//防止出现乱码
$user=$_POST['user'];
$this->verifyCheck();
$Pagemodel = D("user");
$vo = $Pagemodel->create(); 
if(false === $vo) die($Pagemodel->getError());
$topicid = $Pagemodel->add(); //add方法会返回新添加的记录的主键值
if($topicid)
{
//$_SESSION[C('USER_AUTH_KEY')]=$user;//不能用此句
Session::set(C('USER_AUTH_KEY'),$user);
//dump(Session::get('authId')); 
echo "<script>alert(&#39;数据库添加成功&#39;);location.href=&#39;http://127.0.0.1/zhuce/index.php/index&#39;;</script>";
}
else throw_exception("<script>alert(&#39;数据库添加失败&#39;);history.back();</script>");
Copy after login

Step 3: Use if(!Session::is_set(C('USER_AUTH_KEY')))) in the IndexAction.class.php file to determine whether the user is logged in.

Session::get(C('USER_AUTH_KEY')) is to get the name of the logged in user.

public function index()
{
if(!Session::is_set(C(&#39;USER_AUTH_KEY&#39;)))
//if(!isset($_SESSION[&#39;USER_AUTH_KEY&#39;])||($_SESSION[&#39;USER_AUTH_KEY&#39;]==0))//不能用此句
{
$msg="用户没有登录"; 
}
else
{
$msg=Session::get(C(&#39;USER_AUTH_KEY&#39;)).&#39;欢迎你回来&#39;;
}
$this->assign(&#39;msg&#39;,$msg);
$this->display(); 
}
Copy after login

Step 4: Display the template on the homepage, the code is as follows:

<body>
{$msg}<br />
这是我的首页
</body>
Copy after login

The login code is centered around writing session, judging session, and reading session.

Use to write session: Session::set(C('USER_AUTH_KEY'),$user);
Use to judge session: if(!Session::is_set(C('USER_AUTH_KEY')));
To read the session: Session::get(C('USER_AUTH_KEY')) The above is the entire content of the implementation method of ThinkPHP login function

The above is the detailed content of How to implement the login function under the thinkphp framework. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:cnblogs.com
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
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!