Phpsession is a bit messy

Source: Internet
Author: User
Tags php session tmp file tmp folder
Phpsession is a bit confusing. during this time, the number of sessions is quite large. sometimes, I feel confused .... the following are what I think help me to see what I think is & nbsp; what I think is wrong & nbsp; thank you !!!! 1. after session_start () & nbsp; is enabled, the php session will be confused every time a request is sent to the client.
During this time, there are many sessions,
Sometimes I think I understand it, and sometimes I feel confused ....

What I think is
I think it's wrong. thank you !!!!

1. after session_start () is enabled, the corresponding file in the tmp file (assuming linux) will be searched for every request from the client. if there is a matching file, the file will be viewed.

2. if the client cookie contains PHPSESSID (assuming session. name is PHPSESSID) and its value is do07jvm10rtbah6eja5m5q59a1, the corresponding sess_do07jvm10rtbah6eja5m5q59a1 is shown below tmp.

3. set the session to expire. the default value of session. gc_maxlifetime in php. ini is 1440,
After the value is changed to 10, the session will expire in 10 seconds. after the value is 10 seconds (no operation is performed on this session file during this period)
As a result, due to gc probability problems, the session is recycled. when a client request is sent, can the session still exist under tmp?

4. session. gc_probability = 1, session. gc_divisor = 100
That is to say, there is a 1% probability that GC will be started, that is, all session information is scanned, and the last modification time (modified date) of the session is subtracted from the current time, which is the same as the session. compare the gc_maxlifetime parameter. if the survival time exceeds gc_maxlifetime, delete the session (physical deletion)

5. I found no PHPSESSID when I was reading other users' websites because they modified the session. name relationship.



Ah, I can ask so many questions at 100.
It's also a real profit .........
------ Solution --------------------
Ah .. Let me ask you a question ..
Php session loss. Have you ever encountered it?

I have set a session on a page and can print the set session.
But the jump to another page is gone.

For example:
1.php
Session_start ();

$ _ SESSION ['AAA'] = "afdsfd ";
Var_dump ($ _ SESSION );

?>

2.php
Session_start ();

Var_dump ($ _ SESSION );

?>


1. php can be printed, but nothing will happen if you jump to 2. php.


------ Solution --------------------
1. after session_start () is enabled, the corresponding file in the tmp file (assuming linux) will be searched for every request from the client. if there is a matching file, the file will be viewed.
2. if the client cookie contains PHPSESSID (assuming session. name is PHPSESSID) and its value is do07jvm10rtbah6eja5m5q59a1, the corresponding sess_do07jvm10rtbah6eja5m5q59a1 is shown below tmp.

For session_start (), if the client cookie contains PHPSESSID, the corresponding files in the tmp folder (assuming linux) will be searched. if there is a matched file, the file is loaded to $ _ SESSION.

3. set the session to expire. the default value of session. gc_maxlifetime in php. ini is 1440,
After the value is changed to 10, the session will expire in 10 seconds. after the value is 10 seconds (no operation is performed on this session file during this period)
As a result, due to gc probability problems, the session is recycled. when a client request is sent, can the session still exist under tmp?

Yes, but PHPSESSID is passed through the cookie and its validity period is 0.

4. session. gc_probability = 1, session. gc_divisor = 100
That is to say, there is a 1% probability that GC will be started, that is, all session information is scanned, and the last modification time (modified date) of the session is subtracted from the current time, which is the same as the session. compare the gc_maxlifetime parameter. if the survival time exceeds gc_maxlifetime, delete the session (physical deletion)

The correct statement is: when the gc process comes, 1% of the qualified sess _ * files will be deleted.

5. I found no PHPSESSID when I was reading other users' websites because they modified the session. name relationship.
Maybe, or maybe the website is not in php.
------ Solution --------------------
The initial practice of php session is to write data into files. frequent read/write may cause I/O pressure.
My approach is to write the session into memcache.
This is a good practice, with fast read/write speeds and cross-origin access.
I have always used this method.

If you have not installed memcache, you can also write the session into the database. this is also a good practice.

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.