ThinkPHP summary of session operation methods

Source: Internet
Author: User
This article mainly introduces ThinkPHP's session operation methods, which helps readers to deepen their understanding of ThinkPHP's session operations. For more information about ThinkPHP's session operations, see the following article, the details are as follows:

The official ThinkPHP session instructions are as follows:

Start session
Pause session
Clear session
Destroy session
Get session value
GetLocal
Set the session value
SetLocal: Set the private session value
Get or set session_name by name
Is_set whether to set the session value
Is_setLocal: whether to set the private session value
Id to get or set session_id
Get or set session_save_path
SetExpire: Set the session expiration time
SetCookieDomain set valid domain name
SetCallback sets the callback function for Session object deserialization.

The code for the most common operations is as follows:

// Check whether the Session variable has a Session: is_set ('name'); // assign the Session variable value: set ('name', 'value '); // Obtain the Session variable Session: get ('name ');

The Session-related configuration parameter code is as follows:

'Session _ name' => 'thinkid', // Default Session_name 'session _ path' => '', // use the default Session save path 'session _ type' => 'file'. // The Default session type supports DB and File 'session _ EXPIRE '=> '123 ', // The Default Session validity period is 'session _ table' => 'think _ session', // The name of the TABLE in the database SESSION mode is 'session _ callback' => '', // callback method of the deserialization object

Note the SESSION_NAME parameter. if you do not want to share the Session value between different projects, set different values. Otherwise, keep the same default value.
What should I do if I set the same SESSION_NAME value but want to create a project-based private Session space? ThinkPHP also supports private Session operations with the project as the Session Space. taking the previous common operations as an example, we changed the following:

// Check whether the Session variable exists (valid for the current project) Session: is_setLocal ('name'); // assign a value to the Session variable (valid for the current project) Session :: setLocal ('name', 'value'); // Get the Session variable (valid for the current project) Session: getLocal ('name ');

In this way, it does not conflict with global Session operations and can be used for special situations.
ThinkPHP supports database-based Session operations. you can set the SESSION_TYPE value to DB. if you use the database method, make sure that the SESSION_TABLE value is set, import the following DDL statements to your database (using 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,Database Connection in Db Session mode uses the Project database configuration information for connection.In addition to the database mode, you can also add other Session storage mechanisms, such as the memory mode and Memcache mode. we only need to add the corresponding filter and use the session_set_save_handler method, for specific method definitions, refer to Think. util. filterSessionDb under the Filter. class. php file implementation.

Made a simple login judgment
After logon detection, assign the Session value so that the Session value is false if it is not empty.

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

[C ('User _ AUTH_KEY ')] is the built-in method and function class of ThinkPHP. The default value is null when the config. php file is not configured.
Assign the account value retrieved by $ logInFind ['id'] to it. By default, the Session is automatically deleted when the page is closed!
Use the following format to determine other pages

If (! Isset ($ _ SESSION [C ('User _ AUTH_KEY ')]) {// isset checks whether a value is assigned to a variable! $ This-> redirect ('login', 'login'); // go to the registration page}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.