An in-depth understanding of the PHP principle of the session GC for a small probability notice_php tutorial

Source: Internet
Author: User
If you are using apt-installed PHP under Ubuntu/debian, you may have a small chance of encountering this hint when you use the session.
Copy CodeThe 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 session's save handler, then there is a probability that the GC process of the session will run every time session_start.
Copy CodeThe code is 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 that in apt PHP, the default directory for session/VAR/LIB/PHP5 is 733 with sticky bit, which is
Copy CodeThe code is as follows:
DRWX-WX-WT Root Roo

In general, PHP workers are running in a non-root identity, so there is no permission to open this folder (but because can write, so does not affect the normal session file read). The following code in the S_GC, then, triggers the notice at the beginning:
Copy CodeThe 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, under the Ubuntu/debian, there are GC recycling, but the external cron process to complete, the default in/ETC/CRON.D/PHP5:,
Copy CodeThe code is 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 when judging whether S_GC is running: PS (gc_divisor) and PS (gc_probability), which correspond to two configuration items with the same name for the runtime configuration item of the session, respectively:
Session.gc_probability and Session.gc_divisor, they default to 1 and 100 respectively.
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 CodeThe code is as follows:
Rand < Probability/gc_diviso

That is, by default, it is almost 100 times that a GC process can be called. So that's the small probability that you can see this notice.
To close this notice, just set:
session.gc_probability = 0, so that S_GC is not running at all possible.
Of course, you can also change the permissions of this folder ...
Finally, thank cfc4n for providing this question.
Author: laruence ()
This address: http://www.laruence.com/2011/03/29/1949.html

http://www.bkjia.com/PHPjc/323187.html www.bkjia.com true http://www.bkjia.com/PHPjc/323187.html techarticle If you use apt-installed PHP under Ubuntu/debian, you may have a small chance of encountering this hint when using the session. Copy the code code as follows: PHP Notice:session_start () ...

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