PHP session settings that never expire _ PHP Tutorial

Source: Internet
Author: User
Tags php session
Phpsession never expires. This article introduces several methods for setting PHP sessions that never expire. it is the best way to do this without modifying the program, because if you modify the program, the testing department will be very depressed, just like me, this article introduces several php session settings that never expire. it is the best way to do this without modifying the program, because if you modify the program, the testing department will be very depressed, just like me, you can only modify the system environment configuration. In fact, it is very easy to open php. ini setting file

, Modify the three rows as follows:

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:

The code is as follows:

Session_start (); // start the Session
$ _ SESSION ['count']; // registers the Session variable count.
Isset ($ PHPSESSID )? Session_id ($ PHPSESSID): $ PHPSESSID = session_id ();
// If $ PHPSESSID is set, the SessionID is assigned to $ PHPSESSID. Otherwise, the SessionID is generated.

$ _ SESSION ['count'] ++; // variable count Plus 1
Setcookie ('phpsessid ', $ PHPSESSID, time () + 3156000); // store the SessionID to the Cookie
Echo $ count; // display the value of the Session variable count
?>

If it takes a long time (how long? You can see it for yourself.) when you come back to refresh this page, the output number is 1 larger than when you go. that's right! If it's a lot bigger, it's estimated that the computer has moved you. this test is not accurate ...... Go out for a while!

Note: The 'phpsessid' in the setcookie line is not certain. if you have a network administrator with a modified mad disease, he may have modified it, the best way is to use the phpinfo () function to check the session. the value of name is relatively scientific.

Reset session never expires. it is the best way to do this without modifying the program, because if you modify the program, the test department will be very depressed like me, so...

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