PHP set Session details (expiration, expiration, expiration)

Source: Internet
Author: User

Let's take a look at how to set up the session in PHP.ini.

Open php.ini to find one of the following in the Session Settings section:

The code is as follows Copy Code

Session.save_path = "N;/path"
Session.save_path = "C:/temp" #此处以你自己设定的路径为准


This setting provides us with a multilevel hash of the Session Directory, where "N" indicates the level of the directory to be set, followed by "/path" that represents the root path of the session file, such as the format we set to the following

The code is as follows Copy Code

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

The above setting indicates that we have PHP session file for level two directory storage, each level directory is 0-9 and a-Z a total of 36 alphanumeric directory name, so that the directory of the session can reach 36*36, a total of 1332 folders, believe that as a single server, This is completely enough, if your system architecture is designed to share session data with multiple servers, you can increase the directory level to level 3 or more.

Session Expiration Time setting


Continue the session in PHP, in PHP, mainly by setting Session.gc_maxlifetime to set the session life cycle. For example:

The code is as follows Copy Code

<?php
Ini_set (' Session.gc_maxlifetime ', 3600); Set time
Ini_get (' session.gc_maxlifetime ')//Get the Set value in INI
?>

Here's a function that someone else encapsulates, but I haven't tested it for reference only:

The code is as follows Copy Code

<?php
function start_session ($expire = 0)
{
if ($expire = = 0) {
$expire = Ini_get (' session.gc_maxlifetime ');
} else {
Ini_set (' Session.gc_maxlifetime ', $expire);
}

if (Empty ($_cookie[' Phpsessid ')) {
Session_set_cookie_params ($expire);
Session_Start ();
} else {
Session_Start ();
Setcookie (' Phpsessid ', session_id (), time () + $expire);
}
}
?>

How to use:

Add Start_session (600) and expire after//600 seconds.


method of never expiring session


Open php.ini settings file

Modify three lines as follows:

1, Session.use_cookies
Set this value to 1, using a cookie to pass the SessionID

2, Session.cookie_lifetime
This represents the SessionID in the client cookie store time, the default is 0, on behalf of the browser a close sessionid on the void ... That's why PHP's session 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 session data stored on the server side of the time, if more than this time, then the session data will be automatically deleted! So we also set it to 99999999.

It's all OK, of course you don't believe it. Test it--set a session value after a 10-day half-month return to see if your computer is not powered down or down, you can still see the SessionID.

Of course, you may not be able to control the server's permissions and not as lucky as I can modify the php.ini settings, all rely on our own also have a way, of course, we must use the client to store cookies, bar the SessionID stored in the client's cookie, set the value of this cookie, and then the This value is passed to the session_id () function, as follows:

  code is as follows copy code

Session_ Start (); Start session
$_session[' count ']///Register Session variable Count
Isset ($PHPSESSID)? session_id ($PHPSESSID): $PHPSESSID = session_id ();
//If $PHPSESSID is set, assign SessionID to $phpsessid or generate SessionID

$_session[' count ']++//variable count plus 1
Setcookie (' Phpsessid ', $PHPSESSID, Time () +3156000);//save SessionID to Cookies br> Echo $count; Displays 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.