A detailed explanation of session time setting in PHP

Source: Internet
Author: User
In most forums, you can choose when you're logged in, such as saving for one weeks, saving for one months, and so on. You can then set the expiration time of the login via a Cookie. The session expiration time setting mainly has the following several methods:

1. The client does not prohibit cookies

(1) Use session_set_cookie_params () to set the session expiration time, this function is the session with the cookie set expiration time. If you want the Session to expire after one minute, its code example looks like this:

<?php $time = 1*60;                   Set session Expiration Time Session_set_cookie_params ($time);      Use function session_start ();                 Initialize  session $_session["username" = ' tom ';? >

Note: Session_set_cookie_params () must be called before Session_Start ().

Description: This function is not recommended and will cause problems with some browsers. Therefore, it is common to set the failure time manually.

(2) Use the Setcookie () function to set the expiration time for the session, to invalidate the session after one minute, as shown in the following code:

<?php session_start (); Session failure Time $time = 1*60;                     Use Setcookie to manually set the session Expiration Time       Setcookie (Session_name (), session_id (), and time () + $time. ' /');     $_session["username"] = ' tom ';? >

Note: In the example code Setcookie () function, Session_name is the name of the session, SESSION_ID is the identification of the client user, because session_id is a randomly generated unique name, so the session is relatively safe. The expiration time is the same as the expiration time of the cookie, and the last parameter is an optional parameter, which is the path to the cookie.

2. The client prohibits cookies

When the client disables the cookie, the session between the page pass will be invalid, you can block the client cookie as a large supermarket chain, if the card in one of the supermarkets, but there is no network between supermarkets, then the membership card can only be used in the supermarket. How to solve this problem has the following methods to choose from:

(1) It is a practice for many forums to remind users to open cookies before logging in.

(2) Set Session.use_trans_sid = 1 in the php.ini file, or open the-ENABLE-TRANS-SID option at compile time, so that PHP automatically passes session_id across the page.

(3) Hide form delivery session_id by the Get method.

(4) Use the file or database storage session_id, in the transfer between pages manually called.

The 2nd method above is not described in detail because the user cannot modify the php.ini file on the server. The 3rd method we can not use cookies to set the expiration time, but the login situation has not changed. The 4th and most important one is when you develop an enterprise-level website, if you encounter

The session file slows down the server and can be used. Here we introduce the 3rd method of transfer using the GET method, the sample code is as follows, receive the Code of the page header:

<?php  $session _name = Session_name ();        Get session name  $session _id = $_get[$session _name];    Get session_id Get mode  session_id ($session _id);               Key step  session_start ();  $_session[' admin '] = ' soft ';? >

Note: The session is requested after the page will produce a session_id, if the Cookie is not allowed to pass the session_id, the request next page will be re-generated a session_id, which causes the Session to pass between the pages Failure.

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.