A PermissiondeniedNotice solution for PHP Session

Source: Internet
Author: User
Tags php session
This article mainly introduces a PermissiondeniedNotice solution for PHP to use the Session. The system environment in this article is ubuntu and Debian. There is a very small probability that this prompt will be displayed. If you need a friend, you can

This article mainly introduces a Permission denied Notice solution for PHP to use the Session. The system environment in this article is ubuntu and Debian. There is a very small probability that this prompt will be displayed. If you need a friend, you can

If PHP installed with apt is used in ubuntu/Debian, this prompt may be reported when 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


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 rm


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_pisor


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

,

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.