Php session expiration settings

Source: Internet
Author: User
Php session expiration settings
This article introduces how to set the session expiration method in php. the introduction is more detailed. For more information, see.

Set session expiration in php. the simplest thing is to modify session. gc_maxlifetime in the php configuration file.

Summary: each php request has a 1/100 probability (default) to trigger "session reclaim ". If "session recycle" occurs, the/tmp/sess _ * file will be checked. if the last modification time exceeds 1440 seconds (the value of gc_maxlifetime ), delete it, which means that these sessions expire.

1. how does a session exist on the server (generally Apache with PHP module? By default, php stores the session in the/tmp Directory. the file name is as follows: sess_01aab840166fd1dc253e3b4a3f0b8381. Each file corresponds to a session ). More/tmp/sess_01aab840166fd1dc253e3b4a3f0b8381username | s: 9: "jiangfeng"; admin | s: 1: "0"; # variable name | type: Length: value deletes the session file, it indicates that the corresponding session is invalid.

2. how does a session exist on the client (usually a browser? On the browser side, you only need to save the session ID (the unique ID generated by the server. There are two storage methods: cookie and url. If the session ID is saved in the cookie, you can see that there is a PHPSESID variable in the browser's cookie. If the URL is passed, you can see index. php? PHPSESID = 01aab840166fd1dc253e3b4a3f0b8381 URL. (The server uses session. use_cookies to control which method to use)

3. on the server side, how does php determine whether the session file has expired? If the "last modification time" has exceeded "gc_maxlifetime (default value: 1440) seconds, this session file is considered to have expired. during the next session collection, if this file has not been changed, the session file will be deleted (The session will expire ).

If you log on to a website and have not performed any operations within 1440 seconds (default), the corresponding session will be deemed to have expired. Therefore, modify the gc_maxlifetime variable in the php. ini file to extend the session expiration Time: (for example, change the Expiration Time to 86400 seconds) session. gc_maxlifetime = 86400 and then restart the apache server. Note: The recycle mechanism is used for session expiration in php5. The time is set to 86400 seconds. if the session has not been modified within 86400 seconds, it will be deleted during the next "recycle" operation.

3. When will session "recycle" occur? By default, every php request will have a 1/100 probability of recovery, so it may be simply understood as "every 100 php requests will be recycled once ". This probability is controlled by the following parameters: # The probability is gc_probability/gc_pisorsession.gc_probability = 1session. gc_pisor = 100

Note: 1. assume that gc_maxlifetime is 120. if the last modification time of a session file is before 120 seconds, before the next recovery (1/100 probability, this session is still valid. 2. if your session is saved elsewhere in session. save_path, the session recycle mechanism may not automatically process expired session files. In this case, you need to manually (or crontab) delete expired sessions: cd/path/to/sessions; find-cmin + 24 | xargs rm

4. in special cases, because the recycle mechanism checks the "last modification time" of the file, if a session is active, but the session content has not changed, the corresponding session file will not be changed, and the recycle mechanism will deem it a session that has not been active for a long time and delete it.

You can use the following code to solve the problem:

     60)$_SESSION['last_access'] = time();?>

The code attempts to modify the session every 60 seconds.

Conclusion: to modify the session expiration time, modify the variable gc_maxlifetime. The session of php5 adopts a passive collection mechanism (garbage collection ). The expired session file will not disappear, but will be processed by triggering "recycle.

I 'd like to introduce this to you and hope it will help you.

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.