Deep understanding of PHP principles-a small probability of SessionGc Notice

Source: Internet
Author: User
PHPNotice: session_start (): ps_files_cleanup_dir if PHP installed with apt is used in ubuntu/Debian, this prompt may be reported if Session is used.

The code is as follows:


PHP Notice: session_start (): ps_files_cleanup_dir:
Opendir (/var/lib/php5) failed: Permission denied (13)
In/home/laruence/www/htdocs/index. php on line 22


This is because, in PHP, if file_handler is used as the save handler of the Session, the Gc process of the Session will be run at each session_start.

The code is as follows:


// Omitted
Int nrdels =-1;
Nrand = (int) (float) PS (gc_pisor) * php_combined_lcg (TSRMLS_C ));
If (nrand <PS (gc_probability )){
PS (mod)-> s_gc (& PS (mod_data), PS (gc_maxlifetime), & nrdels TSRMLS_CC );
}
// Omitted


The cause of this warning is that in apt PHP, the default session directory/var/lib/php5 has the permission of 733 with sticky bit, that is

The code is as follows:


Drwx-wx-wt root roo


Generally, PHP worker runs in a non-root identity, so it does not have the permission to open this folder (but because it can be written, it does not affect normal Session file reading ). the following code in s_gc triggers the Notice mentioned at the beginning:

The code is as follows:


// For file handler, s_gc indirectly calls ps_files_cleanup_dir:
Dir = opendir (dirname );
If (! Dir ){
Php_error_docref (NULL TSRMLS_CC, E_NOTICE,
"Ps_files_cleanup_dir: opendir (% s) failed: % s (% d )",
Dirname, strerror (errno), errno );
Return (0 );
}


Of course, gc is still available in ubuntu/Debian, but it is completed by external cron processes. the default value is/etc/cron. d/php5 :,

The code is as follows:


* 39 * root [-x/usr/lib/php5/maxlifetime]
& [-D/var/lib/php5] & find/var/lib/php5/
-Type f-cmin + $(/usr/lib/php5/maxlifetime)-print0
| Xargs-n 200-r-0 r


In addition, we can see that when determining whether s_gc is running, there are two key variables: PS (gc_pisor) and PS (gc_probability ), these two variables correspond to the two same-name configuration items of the session runtime configuration item respectively:
Session. gc_probability and session. gc_pisor. the default values are 1 and 100, respectively.
Php_combined_lcg is a random number generator that generates a random number ranging from 0 to 1. Therefore, the above discrimination is equivalent:

The code is as follows:


Rand <probability/gc_piso


That is to say, by default, the gc process can be called almost 100 times. Therefore, the Notice can be seen at a low probability.
To disable this Notice, you only need to set:
Session. gc_probability = 0, so that s_gc has no possibility of running.
Of course, you can also change the permissions of this folder...
Finally, I would like to thank CFC4N for providing this question.
Author: Laruence ()
Address: http://www.laruence.com/2011/03/29/1949.html

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.