An in-depth understanding of the PHP principle session GC's a small probability notice_php technique

Source: Internet
Author: User
If the Ubuntu/debian, using apt installed PHP, then in the session, there may be a small probability of encountering this hint.
Copy Code code 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 you use File_handler as the session's save handler, then there is the probability of running the session's GC process every session_start.
Copy Code code as follows:

Have omitted
int nrdels =-1;
Nrand = (int) (float) PS (gc_divisor) * PHP_COMBINED_LCG (Tsrmls_c));
if (Nrand < PS (gc_probability)) {
PS (MoD)->s_gc (&ps (Mod_data), PS (gc_maxlifetime), &nrdels tsrmls_cc);
Have omitted

The reason for this warning is because in apt PHP, the default directory/VAR/LIB/PHP5 of the session is 733 with sticky bit, which is
Copy Code code as follows:


The general PHP worker is running in non-root status, so there is no permission to open the folder (but because it can write, so does not affect the normal session file read). So the following code in the S_GC triggers the beginning of the notice:
Copy Code code as follows:

For file handler, S_GC calls Ps_files_cleanup_dir indirectly:
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, under Ubuntu/debian, there are GC recoveries, just external cron processes that are done by default in/ETC/CRON.D/PHP5:
Copy Code code as follows:

09,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, you can see that there are two key variables: PS (gc_divisor) and PS (gc_probability), which correspond to the two configuration items of the session's Run-time configuration entry, when the S_GC is judged to be running:
Session.gc_probability and Session.gc_divisor, respectively, default to 1 and 100.
The PHP_COMBINED_LCG is a random number generator that generates random numbers ranging from 0 to 1, so the above discriminant is equivalent to:
Copy Code code as follows:

Rand < Probability/gc_diviso

That is, by default, a GC process can be invoked almost 100 times. So that's a small probability to see this notice.
To turn off this notice, you only need to set:
session.gc_probability = 0, let the s_gc completely not run the possibility can.
Of course, you can also change the permissions of this folder ...
Finally, thank cfc4n for providing this question.
Author: laruence ()
This article address:
Related Article

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: 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.