Home >Backend Development >PHP Tutorial >How to implement the login function under the thinkphp framework

How to implement the login function under the thinkphp framework

不言
不言forward
2018-09-30 14:20:205960browse

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>");

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(); 
}

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

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

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!

Statement:
This article is reproduced at:cnblogs.com. If there is any infringement, please contact admin@php.cn delete