This article describes how to implement the ThinkPHP login function. it is an essential feature for using ThinkPHP for member system development. For more information, see ThinkPHP.
Login is a common function in PHP programming. The ThinkPHP instance in this article mainly enters the home page after successful registration and tells you that it is a login user function. The specific implementation steps are as follows:
Step 1: Add the following to the config. php file:
'USER_AUTH_KEY'=>'authId'
Example:
<? Phpif (! Defined ('think _ path') exit (); return array (// defines the database connection information 'DB _ type' => 'mysql ', // The specified database is mysql 'DB _ host' => 'localhost', 'DB _ name' => 'myuser ', // database name 'DB _ user' => 'root', 'DB _ pwd' => '', // your database connection password 'DB _ port' => '123', 'DB _ prefix' => 'think _', // data table prefix 'user _ AUTH_KEY '=> 'authid');?>
Step 2: Use the insert () code in AdminAction. class. php:
Session::set(C('USER_AUTH_KEY'),$user);
Save the logon username to the session.
The complete implementation code is as follows:
Public function insert () {header ('content-Type: text/html; charset = utf-8 '); // prevents garbled characters $ user = $ _ POST ['user']; $ this-> verifyCheck (); $ Pagemodel = D ("user"); $ vo = $ Pagemodel-> create (); if (false ===$ vo) die ($ Pagemodel-> getError (); $ topicid = $ Pagemodel-> add (); // The add method returns the primary key value of the newly added record if ($ topicid) {// $ _ SESSION [C ('User _ AUTH_KEY ')] = $ USER; // The Session: set (C ('User _ AUTH_KEY') cannot be used '), $ user); // dump (Session: get ('authid'); echo "script" alert ('database added successfully'); location. href = 'http: // 127.0.0.1/zhuce/index. php/index'; script ";} else throw_exception (" script "alert ('database addition failed'); history. back (); script ");}
Step 3: Use if (! Session: is_set (C ('User _ AUTH_KEY ') determines if the USER has logged on.
Session: get (C ('User _ AUTH_KEY ') is the name of the login USER.
The code is as follows:
Public function index () {if (! Session: is_set (C ('User _ AUTH_KEY ') // if (! Isset ($ _ SESSION ['User _ AUTH_KEY ']) | ($ _ SESSION ['User _ AUTH_KEY'] = 0 )) // you cannot use this {$ msg = "the USER has not logged on";} else {$ msg = Session: get (C ('User _ AUTH_KEY ')). 'Welcome Back ';} $ this-> assign ('MSG', $ msg); $ this-> display ();}
Step 4: display the template on the homepage. the code is as follows:
{$ Msg}
This is my homepage
Summary:
The logon code is centered on writing sessions, judging sessions, and reading sessions.
Write session: Session: set (C ('User _ AUTH_KEY '), $ USER );
Used to determine the session: if (! Session: is_set (C ('User _ AUTH_KEY ')));
Read session: Session: get (C ('User _ AUTH_KEY '))
We hope that the implementation of the ThinkPHP login function described in this article can help you in ThinkPHP programming.