Detailed PHP set session (expiration, expiration, expiration) _php skills

Source: Internet
Author: User
Tags php session set time setcookie

In PHP set up a session there are many aspects of the package to set the value of the session or directly set the expiration, expiration and expiration date, the following small series to give you a friend to introduce how to use.

Let's take a look at how to set the session in PHP.ini, open php.ini, look for the following in the session Settings section, the code is as follows:

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" to indicate the root path of the session file, such as the format we set to the following 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, such as the following code:

<?php 
ini_set (' Session.gc_maxlifetime ', 3600);//Set Time 
ini_get (' session.gc_maxlifetime ');//Get the Set value in INI 
?> 

Here is a function that someone else encapsulates, but I have not tested it 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 ')) { 
 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 the PHP.ini settings file and modify the 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:

<?php 
session_start ()//start session 
$_session[' count ';//Register Session variable Count 
isset ($PHPSESSID)? session_id ($PHPSESSID): $PHPSESSID = session_id (); 
If $phpsessid is set, the SessionID is assigned to $PHPSESSID, otherwise the SessionID 
$_session[' count ']++ is generated;//variable count plus 1 
setcookie (' Phpsessid ', $PHPSESSID, Time () +3156000); Stores the echo $count in the cookie SessionID 
;//Displays the value of the session variable count 
?> 

The above is the specific procedure of PHP setting session, the content involves setting the value of the session or setting the expiration, expiration and validity period directly, hope to be helpful to everybody's study.

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.