Summary of ThinkPHP Operation Methods on session

  • 2021-07-09 07:18:45
  • OfStack

This article describes in detail the various operation methods of ThinkPHP on session, as follows:

The official instructions for ThinkPHP operation session are as follows:

start starts session
pause Pause session
clear Clear session
destroy Destroy session
get Get session Value
getLocal Get Private session Value
set Set session Value
setLocal Set Private session Value
name Gets or sets session_name
is_set Set session Value
is_setLocal Set Private session Value
id Gets or sets session_id
path Gets or sets session_save_path
setExpire Set session Expiration Time
setCookieDomain Setting a valid domain name
setCallback sets the callback function when an Session object is deserialized

The most commonly used example code of operation method is as follows:


//  Detection Session Whether the variable exists 
Session::is_set('name');
//  To Session Change   Quantity assignment 
Session::set('name','value');
//  Get Session Variable 
Session::get('name');

The configuration parameter codes related to Session are as follows:


'SESSION_NAME'=>'ThinkID',        //  Default Session_name
'SESSION_PATH'=>'',            //  Adopt the default Session save path
'SESSION_TYPE'=>'File',            //  Default Session Type   Support  DB  And  File 
'SESSION_EXPIRE'=>'300000',        //  Default Session Validity period 
'SESSION_TABLE'=>'think_session',    //  Database Session Mode table name 
'SESSION_CALLBACK'=>'',            //  Callback method for deserializing objects 

The SESSION_NAME parameter should be noted. If the value of passing Session needs not to be shared among different projects, please set different values, otherwise, please keep the same default value.
What if you set the same value for SESSION_NAME, but you want to create a private project-based Session space? ThinkPHP also supports private Session operations with the project as the Session space. Taking the previous common operations as an example, we change them as follows:


//  Detection Session Whether the variable exists (the current project is valid) 
Session::is_setLocal('name');
//  To Session Change   Quantity assignment (current item is valid) 
Session::setLocal('name','value');
//  Get Session Variable (current   Project Valid) 
Session::getLocal('name');

In this way, there is no conflict with the global Session operation, and it can be used for some special cases.
ThinkPHP supports Session operation in database mode. It is OK to set the value of SESSION_TYPE to DB. If using database mode, make sure to set the value of SESSION_TABLE and import the following DDL into your database (take MySQL as an example):


CREATE TABLE `think_session` (
`id` int(11) unsigned NOT NULL auto_increment,
`session_id` varchar(255) NOT NULL,
`session_expires` int(11) NOT NULL,
`session_data` blob,
PRIMARY KEY(`id`)
)

Note that the Db Session database connection uses the database configuration information of the project. In addition to the database mode, we can also add other ways of Session storage mechanism, such as memory mode, Memcache mode, etc. We only need to add corresponding filters, using session_set_save_handler method, and refer to the implementation of FilterSessionDb. class. php file under Think. Util. Filter for specific method definition.

Made a simple login judgment
After landing detection, the value of Session is given, so that the value of Session is not empty, that is, it is false false


$_SESSION[C('USER_AUTH_KEY')] = $logInFind['id'] ;

Where [C ('USER_AUTH_KEY')] is the built-in method and function class of ThinkPHP. Default to null when config. php file is not configured
Give it the account value taken out by $logInFind ['id'], and the default is to close the page Session and delete it automatically!
Other pages are judged using the following format


if(!isset($_SESSION[C('USER_AUTH_KEY')])) { //isset  Is to detect whether a variable is assigned !
   $this->redirect('Login','Login');   // Go to the registration page 
}


Related articles: