Modify the default PHP session time

Source: Internet
Author: User
Tags php session

The default session validity period in PHP is 1440 seconds (24 minutes). What should we do if we want to keep the session from expired? Next we will introduce the best way to do this without modifying the program, if you modify the program, the test department will be very depressed, just like me, so you can only modify the system environment configuration, in fact, very simple, open php. ini setting file,

Modify the three rows as follows:

1. session. use_cookies
Set this value to 1 and use cookie to pass sessionid

2. session. cookie_lifetime
This indicates the time when SessionID is stored in the client Cookie. The default value is 0, indicating that the session ID will be voided once the browser closes it ...... This is why PHP sessions cannot be used permanently! So let's set it to a number that we think is big. How about 999999999? Yes! That's it.

3. session. gc_maxlifetime
This is the time when Session data is stored on the server. If this time is exceeded, Session data will be automatically deleted! Then we set it to 99999999.


If you cannot modify the php. ini file

The Code is as follows: Copy code

<? Php
Session_start (); // start the Session
$ _ SESSION ['Count']; // registers the Session variable count.
Isset ($ PHPSESSID )? Session_id ($ PHPSESSID): $ PHPSESSID = session_id ();
// If $ PHPSESSID is set, the SessionID is assigned to $ PHPSESSID. Otherwise, the SessionID is generated.
 
$ _ SESSION ['Count'] ++; // variable count plus 1
Setcookie ('phpsessid ', $ PHPSESSID, time () + 3156000); // store the SessionID to the Cookie
Echo $ count; // display the value of the Session variable count
?>

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.