Home>Article>Backend Development> 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:20 5822browse

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

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('USER_AUTH_KEY'))) //if(!isset($_SESSION['USER_AUTH_KEY'])||($_SESSION['USER_AUTH_KEY']==0))//不能用此句 { $msg="用户没有登录"; } else { $msg=Session::get(C('USER_AUTH_KEY')).'欢迎你回来'; } $this->assign('msg',$msg); $this->display(); }

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

 {$msg}
这是我的首页

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