PHP session () function How to use _php tutorial

Source: Internet
Author: User
PHP session () function in the PHP development application has a very important role, I would like to give beginners a brief introduction of the PHP session () function using methods and examples.

Compared to the cookie,session is stored on the server side of the session, relatively safe, and does not have the same storage length as the Cookie limit, this article briefly introduces the use of the session.
Since the session is stored as a text file on the server side, the client is not afraid to modify the session content. In fact, the server side of the session file, PHP automatically modify the session file permissions, only the system read and write permissions, and can not be modified by FTP, so much more secure.


Since the session is stored as a text file on the server side, the client is not afraid to modify the contents of the session. In fact, the server side of the session file, PHP automatically modify the session file permissions, only retain the system read and write permissions, and can not be modified by FTP, so a lot of security.

The code is as follows Copy Code

Start session
Session_Start ();
Declare a variable named admin and assign a null value.
$_session["admin"] = null;
?>

Start session
Session_Start ();
Destroys a variable that was originally registered
unset ($_session[' admin ');
Destroying the entire Session file
Session_destroy ();
?>

Start session
Session_Start ();
Save the day
$lifeTime = 24 * 3600;
Setcookie (Session_name (), session_id (), time () + $lifeTime, "/");
?>

Save the day
$lifeTime = 24 * 3600;
Session_set_cookie_params ($lifeTime);
Session_Start ();
$_session["Admin"] = true;
?>

Save the day
$lifeTime = 24 * 3600;
Gets the current Session name, which defaults to PHPSESSID
$sessionName = Session_name ();
Get Session ID
$sessionID = $_get[$sessionName];
Session ID obtained using the session_id () setting
session_id ($sessionID);
Session_set_cookie_params ($lifeTime);
Session_Start ();
$_session[' admin ' = true;
?>

Set up a storage directory
$savePath = './session_save_dir/';
Save the 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 way the session is called.


PHP function Completion: session_id ()

SESSION_ID ()
SESSION_ID () access the current session code.

Syntax: string session_id (string [id]);

This function can be used to obtain or reconfigure the current code for storing the Session. If no parameter ID means that only the current session of the code name, plus the parameter indicates that the session code is set to the new specified ID. Both the input and the return are strings.

Output session_id ()

The code is as follows Copy Code

Session_Start ();
Echo session_id ();
Output dqr58dnuqj2gufvg4o3tmjb9v4
?>

Set session_id ()

The code is as follows Copy Code
session_id ("Nowamagic");
Session_Start ();

Echo session_id ();
Output nowamagic
?>

SESSION_ID resuming session Content
PHP session can be program recovery, this and Java is not the same. The session recovery mechanism can be used to share multiple application sessions because the session of PHP is stored as a file or database. The first is that the session_id is obtained through the session_id () function, and this value can be passed.

Program recovery session, first of all to know session_id, you can know through the manual session recovery through the session_id ($id), but in the recovery should pay attention to a sequence, to get the contents of the previous session, must be in the Session_ Start () executes session_id ($ID) before the session is initialized when the session_start is executed, otherwise you will get an empty session and you will have nothing. The session was re-initialized. This is closely related to the role of Session_Start () because Session_Start tells Php,session to initialize and deserialize the session content from the session file, so Session_ The function of start is to deserialize the contents of the previously stored file. Session_Start be aware of session_id before serialization, and if not, generate a new session_id. Deserializes the contents of the corresponding file, if any.

http://www.bkjia.com/PHPjc/628625.html www.bkjia.com true http://www.bkjia.com/PHPjc/628625.html techarticle PHP session () function in the PHP development application has a very important role, I would like to give beginners a brief introduction of the PHP session () function using methods and examples. Compare the 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.