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:
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('数据库添加成功');location.href='http://127.0.0.1/zhuce/index.php/index';</script>"; } else throw_exception("<script>alert('数据库添加失败');history.back();</script>");
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(); }
<body> {$msg}<br /> 这是我的首页 </body>
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!