Php session settings (expiration, expiration, and validity)

Source: Internet
Author: User
This article mainly introduces how to set the session in php. In fact, there are many questions about setting the session in php, including setting the session value or directly setting the expiration, expiration, and validity period. For more information, see

This article mainly introduces how to set the session in php. In fact, there are many questions about setting the session in php, including setting the session value or directly setting the expiration, expiration, and validity period. For more information, see

In php, there are a lot of questions about setting the session value or directly setting the expiration, expiration, and validity period. The following small series will show you how to use it.

Let's first take a look at how to set the session in php. ini, open php. ini, and find the following in the Session Settings section. The Code is as follows:

Session. save_path = "N;/path" session. save_path = "C:/Temp" # the path you set here prevails.

This setting provides a multi-level hash for the session storage directory. "N" indicates the directory level to be set, the following "/path" indicates the root directory path of the session file. For example, if we set it to the following format, the Code is as follows:

Session. save_path = "2; C:/Temp"

The above settings indicate that we store the php session file in two levels. Each level of directory is 0-9 and a-z with 36 letters and numbers as the directory name, in this way, the directory for storing sessions can reach 36*36, with a total of 1332 folders. I believe this is sufficient for a single server, if your system architecture is designed to share session data among multiple servers, you can increase the directory level to three or more.

Set the Session expiration time

To continue with the Session topic in PHP, we mainly set session. gc_maxlifetime to set the Session lifecycle. For example, the following code:

<? Php ini_set ('session. gc_maxlifetime', 3600); // set the time ini_get ('session. gc_maxlifetime'); // get the value set in ini?>

The following provides a function encapsulated by someone else, but I have not tested it and it is for reference only. The Code is as follows:

<? Php function start_session ($ expire = 0) {if ($ expire = 0) {$ expire = ini_get ('session. gc_maxlifetime');} else {ini_set ('session. gc_maxlifetime', $ expire);} if (emptyempty ($ _ COOKIE ['phpsessid ']) {response ($ expire); session_start ();} else {session_start (); setcookie ('phpsessid ', session_id (), time () + $ expire) ;}}?>

Usage:

Add start_session (600); // expire after 600 seconds.

Methods for never expiring sessions

Open the php. ini setting file and modify the following three lines:

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.

So everything is okay. If you don't believe it, test it. Set a session value for 10 days and half a month, if your computer is not powered off or goes down, you can still see this sessionid.

Of course, you may not be able to modify php just as lucky as you do not have permission to control the server. ini settings, everything depends on ourselves. Of course, we must use the client to store cookies. The sessionID is stored in the client's cookie and the cookie value is set, then pass the value to the session_id () function. The procedure is as follows:

<? Php session_start (); // start Session $ _ SESSION ['Count']; // register the Session variable count isset ($ PHPSESSID )? Session_id ($ PHPSESSID): $ PHPSESSID = session_id (); // If $ PHPSESSID is set, the SessionID is assigned to $ PHPSESSID, otherwise, generate SessionID $ _ SESSION ['Count'] ++; // variable count plus 1 setcookie ('phpsessid ', $ PHPSESSID, time () + 3156000 ); // store the SessionID to the Cookie echo $ count; // display the Session variable count value?>

The above is the specific practice of setting the session in php. The content involves setting the session value or directly setting the expiration, expiration, and validity period. I hope this will be helpful for your learning.

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.