The solution of the session never expires in PHP and its realization method sharing

Source: Internet
Author: User
Tags setcookie

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!
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 pass this value to the function session_id (), as follows:

<?PHPSession_Start();//Start Session$_session[' Count '];//Register Session variable countisset($PHPSESSID)?session_id($PHPSESSID):$PHPSESSID=session_id(); //if $phpsessid is set, the SessionID is assigned to $PHPSESSID, otherwise it is generated SessionID$_session[' Count ']++;//variable count plus 1Setcookie(' Phpsessid ',$PHPSESSID, Time() +3156000);//store SessionID into cookiesEcho $count;//displays the value of the session variable count?>

Note: The ' Phpsessid ' in the Setcookie line is not certain, it is possible to modify it, the best way is to use phpinfo () This function to see, confirm the value of session.name one, more scientific.

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.