Apache/PHP 中会话文件的位置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4927850/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 16:17:20  来源:igfitidea点击:

Location for session files in Apache/PHP

phpapachesession

提问by cambraca

What is the default location of session files on an installation of Apache/PHP on Ubuntu 10.10?

在 Ubuntu 10.10 上安装 Apache/PHP 时会话文件的默认位置是什么?

回答by Gordon

The defaultsession.save_pathis set to ""which will evaluate to your system's temp directory. See this comment at https://bugs.php.net/bug.php?id=26757stating:

默认session.save_path设置为"",这将评估你的系统的临时目录。在https://bugs.php.net/bug.php?id=26757 上查看此评论,说明:

The new default for save_path in upcoming releaess (sic) will be the empty string, which causes the temporary directory to be probed.

即将发布(原文如此)中 save_path 的新默认值将是空字符串,这会导致临时目录被探测。

You can use sys_get_temp_dirto return the directory path used for temporary files

您可以使用sys_get_temp_dir返回用于临时文件的目录路径

To find the currentsession save path, you can use

要查找当前会话保存路径,您可以使用

Refer to this answerto find out what the temp path is when this function returns an empty string.

请参阅此答案以了解此函数返回空字符串时的临时路径是什么。

回答by Rich

First check the value of session.save_pathusing ini_get('session.save_path')or phpinfo(). If that is non-empty, then it will show where the session files are saved. In many scenarios it is empty by default, in which case read on:

首先检查session.save_pathusingini_get('session.save_path')或的值phpinfo()。如果它不是空的,那么它将显示会话文件的保存位置。在许多情况下,默认情况下它是空的,在这种情况下,请继续阅读:

On Ubuntu or Debian machines, if session.save_pathis not set, then session files are saved in /var/lib/php5.

在 Ubuntu 或 Debian 机器上,如果session.save_path未设置,则会话文件保存在/var/lib/php5.

On RHEL and CentOS systems, if session.save_pathis not set, session files will be saved in /var/lib/php/session

在 RHEL 和 CentOS 系统上,如果session.save_path未设置,会话文件将保存在/var/lib/php/session

I think that if you compile PHP from source, then when session.save_pathis not set, session files will be saved in /tmp(I have not tested this myself though).

我认为如果你从源代码编译 PHP,那么 whensession.save_path没有设置,会话文件将被保存在/tmp(虽然我自己没有测试过)。

回答by bshea

If unsure of compiled default, look at the pertinent php.ini.
Normally, this will show the commented out default value.

如果不确定编译的默认值,请查看相关的php.ini.
通常,这将显示注释掉的默认值。

Ubuntu/Debian old/new php.inilocations:
Older php5 with Apache: /etc/php5/apache2/php.ini
Older php5 with NGINX+FPM: /etc/php5/fpm/php.ini
Ubuntu 16+ with Apache: /etc/php/*/apache2/php.ini*
Ubuntu 16+ with NGINX+FPM - /etc/php/*/fpm/php.ini*

Ubuntu/Debian 旧/新php.ini位置:
带有 Apache 的/etc/php5/apache2/php.ini
旧 php5:带有 NGINX+FPM 的旧 php5:/etc/php5/fpm/php.ini
带有 Apache 的 Ubuntu 16+ :/etc/php/*/apache2/php.ini*
带有 NGINX+FPM 的 Ubuntu 16+ - /etc/php/*/fpm/php.ini*

* /*/= the current PHP version(s) installed on system.

* /*/= 系统上安装的当前 PHP 版本。

To show the PHP version in useunder Apache:

要显示在 Apache 下使用的 PHP 版本:

$ a2query -m | grep "php" | grep -Eo "[0-9]+\.[0-9]+"

7.3

$ a2query -m | grep "php" | grep -Eo "[0-9]+\.[0-9]+"

7.3

Since PHP 7.3 is the version running for this example, you would use that for the php.ini:

由于 PHP 7.3 是本示例运行的版本,因此您可以将其用于php.ini

$ grep "session.save_path" /etc/php/7.3/apache2/php.ini

;session.save_path = "/var/lib/php/sessions"

$ grep "session.save_path" /etc/php/7.3/apache2/php.ini

;session.save_path = "/var/lib/php/sessions"

Or, combined one-liner:

或者,组合单线:

$ APACHEPHPVER=$(a2query -m | grep "php" | grep -Eo "[0-9]+\.[0-9]+") \ && grep ";session.save_path" /etc/php/${APACHEPHPVER}/apache2/php.ini

$ APACHEPHPVER=$(a2query -m | grep "php" | grep -Eo "[0-9]+\.[0-9]+") \ && grep ";session.save_path" /etc/php/${APACHEPHPVER}/apache2/php.ini

Result:

结果:

;session.save_path = "/var/lib/php/sessions"

;session.save_path = "/var/lib/php/sessions"



Or, use PHP itself to grab the value using the "cli" environment (see NOTEbelow):

或者,使用 PHP 本身通过“cli”环境获取值(请参见下面的注释):

$ php -r 'echo session_save_path() . "\n";'
/var/lib/php/sessions
$

These will also work:

这些也将起作用:

php -i | grep session.save_path

php -r 'echo phpinfo();' | grep session.save_path

NOTE:

笔记:

The 'cli' (command line) version of php.ininormallyhas the same default values as the Apache2/FPM versions (at least as far as the session.save_path). You could also use a similar command to echo the web server's current PHP module settings to a webpage and use wget/curl to grab the info. There are many postsregarding phpinfo()use in this regard. But, it is quicker to just use the PHP interface or grepfor it in the correct php.inito show it's default value.

'cli'(命令行)版本php.ini通常具有与 Apache2/FPM 版本相同的默认值(至少就 而言session.save_path)。您还可以使用类似的命令将 Web 服务器的当前 PHP 模块设置回显到网页,并使用 wget/curl 获取信息。有很多的职位有关phpinfo()这方面的使用。但是,仅使用 PHP 界面或grep正确php.ini显示它的默认值会更快。

EDIT: Per @aesede comment -> Added php -i. Thanks

编辑:根据@aesede 评论 -> 添加php -i. 谢谢

回答by Kevin Borders

Another common default location besides /tmp/is /var/lib/php5/

除此之外,另一个常见的默认位置/tmp//var/lib/php5/

回答by themarketka

I had the same trouble finding out the correct path for sessions on a Mac. All in all, I found out that the CLI PHP has a different temporary directory than the Apache module: Apache used /var/tmp, while CLI used something like /var/folders/kf/hk_dyn7s2z9bh7y_j59cmb3m0000gn/T. But both ways, sys_get_temp_dir()got me the right path when session.save_pathis empty. Using PHP 5.5.4.

我在寻找 Mac 上会话的正确路径时遇到了同样的问题。总而言之,我发现 CLI PHP 具有与 Apache 模块不同的临时目录:Apache used /var/tmp,而 CLI 使用类似/var/folders/kf/hk_dyn7s2z9bh7y_j59cmb3m0000gn/T. 但无论哪种方式,sys_get_temp_dir()session.save_path空无一人时,都让我找到了正确的道路。使用 PHP 5.5.4。

回答by tanius

The only surefire option to find the current session.save_pathvalue is always to check with phpinfo()in exactly the environment where you want to find out the session storage directory.

查找当前session.save_path值的唯一可靠选项是始终phpinfo()在您要查找会话存储目录的环境中进行检查。

Reason: there can be all sorts of things that change session.save_path, either by overriding the php.inivalue or by setting it at runtime with ini_set('session.save_path','/path/to/folder');. For example, web server management panels like ISPConfig, Plesk etc. often adapt this to give each website its own directory with session files.

原因:可以session.save_path通过覆盖php.ini值或在运行时使用ini_set('session.save_path','/path/to/folder');. 例如,ISPConfig、Plesk 等 Web 服务器管理面板通常会对此进行调整,以便为每个网站提供自己的包含会话文件的目录。

回答by Tim

Non of the above worked for me using the IUS repo for CentOS 7 with PHP 7.2:

使用 PHP 7.2 的 CentOS 7 的 IUS 存储库,以上都不对我有用:

php -v
> PHP 7.2.30 (cli) (built: Apr 19 2020 00:32:29) ( NTS )

php -r 'echo session_save_path(), "\n";
> 

php -r 'echo sys_get_temp_dir(), "\n";'
> /tmp

However, sessions weren't saved in the /tmpfolder, but in the /var/lib/php/mod_php/session/folder:

但是,会话并未保存在/tmp文件夹中,而是保存在文件/var/lib/php/mod_php/session/夹中:

ls /var/lib/php/mod_php/session/
> sess_3cebqoq314pcnc2jgqiu840h0k  sess_ck5dtaerol28fpctj6nutbn6fn  sess_i24lgt2v2l58op5kfmj1k6qb3h  sess_nek5q1alop8fkt84gliie91703
> sess_9ff74f4q5ihccnv6com2a8409t  sess_dvrt9fmfuolr8bqt9efdpcbj0d  sess_igdaksn26hm1s5nfvtjfb53pl7  sess_tgf5b7gkgno8kuvl966l9ce7nn

回答by profitphp

I believe its in /tmp/. Check your phpinfo function though, it should say session.save_path in there somewhere.

我相信它在 /tmp/ 中。不过,请检查您的 phpinfo 函数,它应该在某处显示 session.save_path。

回答by blanknamefornow

Depending on the type of server you're running it can vary. To locate the directory, search for the following in your php.ini file.

根据您运行的服务器类型,它可能会有所不同。要找到该目录,请在 php.ini 文件中搜索以下内容。

upload_tmp_dir = "c:/wamp/tmp"

upload_tmp_dir = "c:/wamp/tmp"

The directory can be different for you.

您的目录可以不同。