How does php set a session that strictly controls the expiration time?

Source: Internet
Author: User
Tags php session

How does php set a session that strictly controls the expiration time?

1. php session Validity Period

The default session Validity Period of PHP is 1440 seconds (24 minutes). If the client does not Refresh after 24 minutes, the current session will be recycled and will become invalid.

When the user closes the browser and the session ends, the session will also become invalid.

You can modify the session. gc_maxlifetime of php. ini to set the session lifecycle. However, the session information cannot be deleted immediately after this period. Because GC is started at a probability, it may not be started for a long time. Therefore, a large number of sessions remain valid after session. gc_maxlifetime is exceeded.

2. session. gc_maxlifetime, session. gc_probability, session. gc_divisor description

Session. gc_maxlifetime = 30 indicates that when the session file is not accessed after 30 seconds, it is regarded as an expired session and will be recycled by GC.

The probability of GC Process calling is calculated by session. gc_probability/session. gc_divisor, and session. gc_divisor is 1000 by default,

If session. gc_probability = 1000, the GC process will call and recycle each time session_start () is executed.

Increasing the probability of session. gc_probability/session. gc_divisor will help, but it will seriously affect the performance.

3. strictly control the session expiration Method

1. Use memcache/Redis to save the session and set the expiration time. Because the recovery mechanism of memcache/redis is not based on the probability, it can ensure that the session will expire after expiration.

2. Use php only to create a session class and write the expiration time when the session is written. When reading data, you can determine whether the data has expired Based on the expiration time.

<? Php/*** Session control class */class Session {/*** set session * @ param String $ name session name * @ param Mixed $ data session data * @ param Int $ expire timeout (seconds) */public static function set ($ name, $ data, $ expire = 600) {$ session_data = array (); $ session_data ['data'] = $ data; $ session_data ['expire '] = time () + $ expire; $ _ SESSION [$ name] = $ session_data ;} /*** read session ** @ param String $ name session name * @ retu Rn Mixed */public static function get ($ name) {if (isset ($ _ SESSION [$ name]) {if ($ _ SESSION [$ name] ['expire ']> time () {return $ _ SESSION [$ name] ['data'];} else {self:: clear ($ name) ;}} return false;}/*** clear session * @ param String $ name session name */private static function clear ($ name) {unset ($ _ SESSION [$ name]) ;}}?>

Demo:

<? Phpsession_start (); $ data = '000000'; session: set ('test', $ data, 10); echo session: get ('test '); // output sleep (10); echo session: get ('test'); // expired?>

The above is all the content of this article. I hope this article will help you in your study or work. I also hope to provide more support to the customer's home!

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.