Phpsession () function usage _ PHP Tutorial

Source: Internet
Author: User
Phpsession () function usage. The phpsession () function has a very important role in php application development. next I will give a brief introduction to the usage and examples of phpsession () functions for beginners. Comparing the Coo php session () function, which has a very important role in php development and application, I will give a brief introduction to the usage and examples of php session () functions.

Compared with cookies, Session is stored on the server, which is relatively secure and has no storage length limit as Cookie does. This article briefly introduces the use of Session.
The Session is stored on the server as a text file, so the client is not afraid to modify the Session content. In fact, in the Session file on the server side, PHP automatically modifies the Session file permissions, only retaining the system read and write permissions, and cannot be modified through ftp, which is much safer.


The Session is stored on the server as a text file, so the client is not afraid to modify the Session content. In fact, in the Session file on the server side, PHP automatically modifies the Session file's permissions, only retaining the system's read and write permissions, and cannot be modified through ftp, so it is much safer.

The code is as follows:

// Start the Session
Session_start ();
// Declare a variable named admin and assign a null value.
$ _ SESSION ["admin"] = null;
?>

// Start the Session
Session_start ();
// Destroy a previously registered variable
Unset ($ _ SESSION ['admin']);
// Destroy the entire Session file
Session_destroy ();
?>

// Start the Session
Session_start ();
// Save for one day
$ LifeTime = 24*3600;
Setcookie (session_name (), session_id (), time () + $ lifeTime ,"/");
?>

// Save for one day
$ LifeTime = 24*3600;
Session_set_cookie_params ($ lifeTime );
Session_start ();
$ _ SESSION ["admin"] = true;
?>

// Save for one day
$ LifeTime = 24*3600;
// Obtain the current Session name. The default value is PHPSESSID.
$ SessionName = session_name ();
// Obtain the Session ID
$ SessionID = $ _ GET [$ sessionName];
// Use session_id () to set the Session ID
Session_id ($ sessionID );
Session_set_cookie_params ($ lifeTime );
Session_start ();
$ _ SESSION ['admin'] = true;
?>

// Set a storage directory
$ SavePath = './session_save_dir /';
// Save for one day
$ LifeTime = 24*3600;
Session_save_path ($ savePath );
Session_set_cookie_params ($ lifeTime );
Session_start ();
$ _ SESSION ['admin'] = true;
?>

In addition, we can use the session_set_save_handler function to customize the Session call method.


PHP function completion: session_id ()

Session_id ()
Session_id () to access the current session code.

Syntax: string session_id (string [id]);

This function can obtain or reconfigure the current Session code. If no parameter id is provided, only the current Session code is obtained. if the parameter is added, the Session code is set to the New specified id. The input and return parameters are strings.

Output session_id ()

The code is as follows:

Session_start ();
Echo session_id ();
// Output dqr58dnuqj2gufvg4o3tmjb9v4
?>

Set session_id ()

The code is as follows:
Session_id ("NowaMagic ");
Session_start ();

Echo session_id ();
// Output NowaMagic
?>

Session_id resume session content
Php session can be recovered by the program, which is not the same as java. The session recovery mechanism can be used to share sessions of multiple applications, because php sessions are stored in the form of files or databases. First, the session_id is obtained through the session_id () function. this value can be passed.

To resume a session, you must first know the session_id. you can use the manual to know that the session is restored by session_id ($ id). However, you must pay attention to the sequence when restoring the session, to obtain the content of the previous session, you must execute session_id ($ id) before session_start () so that the previous content can be restored when the session is initialized when session_start is executed, otherwise, you will get an empty session and you will not get anything. Previously, the session was reinitialized. This is closely related to the role of session_start (), because session_start tells php that the session should be initialized and deserialized from the session file, therefore, session_start is used to deserialize the previously stored file content. Before serialization, session_start needs to know the session_id. If no session_id is generated, a new one is generated. If yes, deserialize the content of the corresponding file.

Http://www.bkjia.com/PHPjc/628625.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/628625.htmlTechArticlephp session () function in php development and application has a very important role, I will give beginners a brief introduction to the use of php session () function and instances. Compare Coo...

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.