Detailed PHP Setup session (expiry, expiration, expiration), detailed session_php tutorial

Source: Internet
Author: User
Tags php session

PHP Setup session (expiration, expiration, expiration), detailed session

Set the session in PHP there are many aspects of the package has to set the value of the session or directly set the expiration, expiration and validity period, the following small series to give you friends to introduce how to use.

Let's take a look at how the session is set in PHP.ini, open php.ini, find 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 multi-level hash of the session's directory, where "N" indicates the number of levels to set, and the following "/path" indicates the path to the root directory where the session file is stored, such as the format we set as below:

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

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

Session Expiration Time setting

Continue the session topic in PHP, in PHP mainly by setting the Session.gc_maxlifetime to set the session life cycle, such as the following code:

Here is a function that someone else has encapsulated, 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),//600 seconds later expires.

The session never Expires method

Open the PHP.ini settings file and modify the three line as follows:

1, Session.use_cookies

Set this value to 1 and use a cookie to pass the SessionID

2, Session.cookie_lifetime

This represents sessionid the time that the client cookie is stored, the default is 0, which means that the browser shuts down SessionID. This is why the session of PHP cannot be used permanently! So let's set it to a number we think is big, 999999999 how, yes! That's it.

3, Session.gc_maxlifetime

This is the session data on the server side of the storage time, if more than this time, then the session data will be automatically deleted! Then we also set it to 99999999.

That's all OK, of course, if you don't believe it, just 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 this sessionid.

Of course, you may not have control of the server permissions and not as fortunate as I can modify the php.ini settings, all depend on ourselves there is a way, of course, you must use the client to store cookies, the resulting 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:

The above is the specific procedure of the PHP setup session, the content of the session setting value or directly set the expiration, expiration and expiration, I hope that everyone's learning to help. true techarticle detailed PHP settings session (expiration, expiration, expiration), detailed session in PHP set session There are a lot of aspects of the session set the value or directly set the expiration, expiry and expiration, the following small ...

  • Related Article

    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: 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.