PHP会话保存路径异常:生产服务器仍指向WAMP本地目录
我在本地使用WAMP开发了一个网站,将文件上传到生产服务器后,测试账户系统时发现PHP始终将会话保存到本地WAMP的文件夹(路径为E:/wamp64/tmp),但该目录在生产服务器上并不存在。我的php.ini中已指定使用cPanel的tmp文件夹,且PHP脚本中未设置其他会话保存路径,请问这是什么原因导致的?
我的php.ini配置
; cPanel-generated php ini directives, do not edit ; Manual editing of this file may result in unexpected behavior. ; To make changes to this file, use the cPanel MultiPHP INI Editor (Home >> Software >> MultiPHP INI Editor) ; For more information, read our documentation (https://go.cpanel.net/EA4ModifyINI) allow_url_fopen = On error_reporting = E_ALL log_errors = On error_log = "/home/seffcraf/public_html/statusmc.seffcraft.eu/php_errors.log" detect_unicode = Off display_errors = On enable_dl = On file_uploads = On max_execution_time = 30 max_input_time = 60 max_input_vars = 1000 memory_limit = 32M post_max_size = 8M session.gc_maxlifetime = 1440 session.save_path = "/home/seffcra1/public_html/statusmc.seffcraft.eu/tmp" upload_max_filesize = 2M zlib.output_compression = Off extensions=sockets
错误信息
Warning: session_start(): open(E:/wamp64/tmp/sess_fg79n0c5ek7vfpp2e5n43m94ct, O_RDWR) failed: No such file or directory (2) Warning: session_start(): Failed to read session data: files (path: E:/wamp64/tmp)
可能的原因及解决办法
php.ini配置未生效:
cPanel服务器中,手动修改的php.ini可能未被当前网站使用的PHP版本读取。先在网站根目录创建一个phpinfo.php文件,内容为<?php phpinfo(); ?>,访问该文件后查找session.save_path项,确认实际生效的路径是否为你设置的cPanel tmp目录。如果不符,需通过cPanel的MultiPHP INI Editor选择对应PHP版本的配置文件进行修改,修改后联系主机商重启PHP服务(部分cPanel主机支持后台自行重启)。存在覆盖配置的文件:
检查服务器网站根目录下的.htaccess或.user.ini文件,看是否存在php_value session.save_path这类语句,手动指定了本地WAMP的路径。如果有,删除或修改为生产服务器的tmp路径。目标tmp目录权限/存在性问题:
确认你在php.ini中指定的/home/seffcra1/public_html/statusmc.seffcraft.eu/tmp目录是否存在,且权限设置正确。该目录需确保PHP进程拥有读写权限,一般设置权限为755,所有者需与网站运行用户一致(如cPanel账户名seffcra1)。若目录不存在,先通过文件管理器或FTP创建。PHP版本不匹配:
检查cPanel的MultiPHP Manager,确认当前网站绑定的PHP版本,是否与你修改php.ini对应的PHP版本一致。比如你修改了PHP7.4的配置,但网站实际使用的是PHP8.0,那么配置不会生效,需修改对应版本的php.ini文件。
内容的提问来源于stack exchange,提问作者Nimetu

