当前位置: 首页 > 工具软件 > Start Me Up > 使用案例 >

[乐意黎原创]PHP启用session后抛 session_start(): open(/var/lib/php/session/sess_... 的解决办法

越朗
2023-12-01

如题,PHP启用SESSION后抛 

Warning: session_start(): open(/var/lib/php/session_user/sess_d5gn9q7q9qii26ajk2c8ltrefs, O_RDWR) failed: No such file or directory (2) in /data/webDev/websites/daza.ren/session-util.phpon line 3

Warning: session_start(): Failed to read session data: files (path: /var/lib/php/session) in /data/webDev/websites/daza.ren/session-util.php on line 3

以及下面权限方面的错误,

Warning: Unknown: open(/usr/local/temp/sess_ho0i0q5ircrvu1h3i2fa4n3df0, O_RDWR) failed: Permission denied (13) in Unknown on line 0

Warning: Unknown: Failed to read session data: files (path: /usr/local/temp) in Unknown on line 0

Warning: session_start(): open(/usr/local/temp/sess_ho0i0q5ircrvu1h3i2fa4n3df0, O_RDWR) failed: Permission denied (13) in /data/webDev/websites/daza.ren/session-util.php on line 3

Warning: session_start(): Failed to read session data: files (path: /usr/local/temp) in /data/webDev/websites/daza.ren/session-util.php on line 3

----------------------------------------------------------

解决办法:  修改 php.ini  (whereis php.ini) 查找

1. 修改下面的 session.save_path 为实际设定路径。如下:

1354 ; where MODE is the octal representation of the mode. Note that this
1355 ; does not overwrite the process's umask.
1356 ; http://php.net/session.save-path
1357 ;session.save_path = "/tmp"
1358 session.save_path = "/usr/local/php/temp"

 2. 修改下面的 session.gc_maxlifetim为实际时间。如下:

1435 ; After this number of seconds, stored data will be seen as 'garbage' and
1436 ; cleaned up by the garbage collection process.
1437 ; http://php.net/session.gc-maxlifetime
1438 session.gc_maxlifetime = 3600

  以及

; Initialize session on request startup.
; http://php.net/session.auto-start
; session.auto_start = 0
session.auto_start = 1

由于我使用的是 php-fpm,  session.save_path 在 /etc/php.ini中设置后不生效,

Warning: session_start(): open(/var/lib/php/session_user/sess_d5gn9q7q9qii26ajk2c8ltrefs, O_RDWR) failed: No such file or directory (2) in /data/webDev/websites/daza.ren/session-util.phpon line 3
抛错实际生效的地方是 
/usr/local/php/etc/php-fpm.d/www.conf


410 ; For php_*flag, valid values are on, off, 1, 0, true, false, yes or no.
411 ; 2018-11-08
412 php_value[session.save_handler] = files
413 ; php_value[session.save_path] = /var/lib/php/session
414 php_value[session.save_path] = /usr/local/temp

 

3. 创建 "/usr/local/php/temp" 目录并确保权限

//查看权限
ls -ld /usr/local/temp

//以下 user和user-group 请根据实际填写 
chown -R user:user-group /usr/local/temp && chmod 777 -R /usr/local/temp

 

4. 重启 php-fpm 和 nginx

php-fpm:

#启动服务     service php-fpm start 
#停止服务     service php-fpm stop 

nginx:

#启动服务     nginx -s stop
#启动服务     nginx

 


其它参考:

use session_save_path() on page starting

refer : http://php.net/manual/en/function.session-save-path.php

I eventually managed to get it working by putting the full path of my new folder in..

session_save_path('/home/scittwebhost/sessions');

I still do not know why I couldn't write to /tmp - the permissions were 777

Thanks all

 

乐意黎原创

2019-07-09

 类似资料: