A bit of experience in PHP session usage summary _php skills

Source: Internet
Author: User
When session sessions are turned on, a cookie (named Phpsessid can be obtained by Session_name () is first sent to the browser's unique identifier session_id), with Session.save_handler = In the case of files, a file without a suffix is generated under the specified directory of the server (such as temp), and the name is
Copy Code code as follows:

' Sess_ ' + ' session_id ';


This completes the basic setup. The next time the HTTP request is launched, the browser will first send all the cookie names and values under the current domain name, so that the server can read the session file according to the session_id in the cookie without confusing who the session belongs to.

This step is detailed as follows:

The session sends a unique cookie variable session_id to the browser, and the session_id variable has a name and a value. The variable name (name) defaults to PHPSESSID, and the variable value (value) is a randomly generated string of Apach, similar to rvag9m368vim7k8g4v7k2ank70. Usually said session_id actually refers to this unique string rvag9m368vim7k8g4v7k2ank70.

Specifically under the HTTP response header of FF:


Session_Start ();
The above function is completed in the program, if the browser does not send a PHPSESSID cookie to send a past, have read this cookie, so that you can maintain the same session.

Okay, now that we know the working principle of the session, we can speculate that if the server side sess_rvag9m368vim7k8g4v7k2ank70 This file we manually deleted, then session failure, if the browser cookie invalidation, Then the session still fails.
In the case of manual:
On the server side, you can use the
Session_ destroy () or session_ unset ()
To make it ineffective.
On the browser side:
Can directly
? Setcookie (' Phpsessid ', ', ', 123);
Let the cookie expire, or another way, but not immediately invalid
Session_set_cookie_params ($time);//The seconds on the current timestamp, such as 60, which expires after 60 seconds, <span style= "COLOR: #ff0000" > Do not use timestamp + your own set of time. </SPAN>
All of the above is to let the session expired prematurely, but want to directly let the session delay it do not? In addition to modifying the configuration (Session.gc_maxlifetime) is not possible, in the php.ini inside Session.gc_maxlifetime set expiration time, at this time, there are session.gc_probability The probability of session.gc_divisor is recycled. If this is the time, and the GC process is started, the GC will read the session file's modification time (mtime), and find that it is more than session.gc_maxlifetime after subtracting from the current time, and immediately deletes it. To this, we also understand how to maintain this session, only in the session.gc_maxlifetime, must have the user in the visit, each visit to modify session, so that the sessions and more session.gc_ The survival time of the maxlifetime.
In addition to say Session.cookie_lifetime, set PHPSESSID in the browser's survival time, the default for 0,ie I found that it is normal, browser restart that is the cookie invalidation, FF also continues to exist. Set Session.cookie_lifetime can use Session_set_cookie_params,
(session_set_cookie_params);//60 s session_start ();
Session.gc_maxlifetime and Session.cookie_lifetime together determine the life time of the session.
-------------------------------------------------------------
Just looked for the Firefox cookie session expired information, found as follows

This is apparently by design. Check out this Bugzilla bug:https://bugzilla.mozilla.org/show_bug.cgi?id=443354

Firefox has a feature where you close Firefox and it offers to save all your tabs, and then you restore the browser and th OSE tabs come back. That ' s called session restore. What I didn ' t realize is, it ' ll also restore all the sessions cookies for those pages too! It treats it like you had never closed the browser.

This makes sense in the sense so if your browser crashed you have right back to where and were, but is a little disconcer Ting for web devs used to session cookies getting cleared. I ' ve got some old sessions cookies from months ago this were set by sites I always have open in tabs.

To test this out, close the tabs in your browser, then close the browser and restart it. I am the session cookie for your site should clear in. Otherwise you ' d have to turn off the session restore.

This is the Firefox session save function, FF design is so. You can do this close to the tabs in your browser, then close the browser and restart it test to see if it is still saved.

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.