Home >Backend Development >PHP Tutorial >In-depth understanding of PHP principles: a small probability of Session Gc Notice_PHP Tutorial

In-depth understanding of PHP principles: a small probability of Session Gc Notice_PHP Tutorial

WBOY
WBOYOriginal
2016-07-21 15:30:45812browse

If you use PHP installed with apt under ubuntu/Debian, you may encounter this prompt with a small probability when using Session.

Copy code 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 you use file_handler as the save handler of the Session, then there is a probability that the Gc of the Session will be run every time session_start Process.
Copy code The code is as follows:

//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);
}
//Omitted

The reason for this warning is because in apt's PHP, the default directory of session /var/lib The permissions of /php5 are 733 with sticky bit, that is,
Copy the code The code is as follows:

drwx-wx-wt root roo

Generally, PHP workers run under non-root identities, so they do not have permission to open this folder (but because it can write, it does not affect normal Session file reading). So the following code in s_gc will trigger the Notice mentioned at the beginning:
Copy the code The code is as follows:

/ /For the 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, in ubuntu Under /Debian, there is still gc recycling, but it is done by an external cron process. The default is /etc/cron.d/php5:,
Copy code The 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 when determining whether s_gc is running, there are two key variables: PS (gc_divisor) and PS (gc_probability). These two variables correspond to the two same-named runtime configuration items of the session. Configuration items:
session.gc_probability and session.gc_divisor, they default to 1 and 100 respectively.
And php_combined_lcg is a random number generator that generates random numbers in the range of 0 to 1, so the above discrimination is equivalent to:
Copy code The code is as follows:

rand < probability / gc_diviso

That is to say , by default, the gc process can be called almost once every 100 times. Therefore, there is a small probability that you can see this Notice.
To close this Notice, you only need to set:
session.gc_probability = 0, let s_gc There is no possibility of running at all.
Of course, you can also change the permissions of this folder...
Finally, thank CFC4N for providing this question.
Author: Laruence( )
This article address: http: //www.laruence.com/2011/03/29/1949.html

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/323187.htmlTechArticleIf you use apt-installed PHP under ubuntu/Debian, there may be problems when using Session There is a small chance of encountering this prompt. Copy the code as follows: PHP Notice: session_start()...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn