php $_SESSION 变量存储在哪里?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/454635/
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
Where are $_SESSION variables stored?
提问by Steve Gattuso
Are $_SESSIONvariables stored on the client or the server?
被$_SESSION存储在客户端或服务器上的变量?
回答by rjamestaylor
The location of the $_SESSIONvariable storage is determined by PHP's session.save_pathconfiguration. Usually this is /tmpon a Linux/Unix system. Use the phpinfo()function to view your particular settings if not 100% sure by creating a file with this content in the DocumentRootof your domain:
$_SESSION变量存储的位置由 PHP 的session.save_path配置决定。通常这是/tmp在 Linux/Unix 系统上。phpinfo()如果不是 100% 确定,请使用该功能查看您的特定设置,方法是DocumentRoot在您的域中创建一个包含此内容的文件:
<?php
phpinfo();
?>
Here is the link to the PHP documentation on this configuration setting:
这是有关此配置设置的 PHP 文档的链接:
http://php.net/manual/en/session.configuration.php#ini.session.save-path
http://php.net/manual/en/session.configuration.php#ini.session.save-path
回答by troelskn
As mentioned already, the contents are stored at the server. However the session is identified by a session-id, which is stored at the client and send with each request. Usually the session-id is stored in a cookie, but it can also be appended to urls. (That's the PHPSESSIDquery-parameter you some times see)
如前所述,内容存储在服务器上。但是,会话由会话 ID 标识,该 ID 存储在客户端并随每个请求一起发送。通常 session-id 存储在 cookie 中,但它也可以附加到 url。(这是PHPSESSID您有时会看到的查询参数)
回答by Rob Kennedy
They're generally stored on the server. Where they're stored is up to you as the developer. You can use the session.save_handlerconfiguration variable and the session_set_save_handlerto control how sessions get saved on the server. The default save method is to save sessions to files. Where they get saved is controlled by the session.save_pathvariable.
它们通常存储在服务器上。它们的存储位置取决于您作为开发人员。您可以使用session.save_handler配置变量和session_set_save_handler控制会话在服务器上的保存方式。默认的保存方法是将会话保存到文件中。它们保存的位置由session.save_path变量控制。
回答by Gero
One addition: It should be noted that, in case "/tmp" is the directory where the session data is stored (which seems to be the default value), the sessions will not persist after reboot of that web server, as "/tmp" is often purged during reboot. The concept of a client-wise persistence stands and falls with the persistence of the storage on the server - which might fail if the "/tmp" directory is used for session data.
补充一点:需要注意的是,如果“/tmp”是存储会话数据的目录(这似乎是默认值),则在该 Web 服务器重新启动后,会话将不会持续存在,如“/tmp” " 经常在重启过程中被清除。客户端持久性的概念随着服务器上存储的持久性而成立和下降 - 如果“/ tmp”目录用于会话数据,则可能会失败。
回答by Hans
On Debian (isn't this the case for most Linux distros?), it's saved in /var/lib/php5/. As mentioned above, it's configured in your php.ini.
在 Debian 上(大多数 Linux 发行版不是这样吗?),它保存在 /var/lib/php5/ 中。如上所述,它是在您的 php.ini 中配置的。
回答by Vasyl Teraz
I am using Ubuntu and my sessions are stored in /var/lib/php5.
我正在使用 Ubuntu,我的会话存储在 /var/lib/php5 中。
回答by Brian Fisher
As Mr. Taylor pointed out this is usually set in php.ini. Usually they are stored as files in a specific directory.
正如 Taylor 先生所指出的,这通常是在 php.ini 中设置的。通常它们作为文件存储在特定目录中。
回答by Luká? K?í?
For ubuntu 16.10 are sessions save in /var/lib/php/session/...
对于 ubuntu 16.10,会话保存在 /var/lib/php/session/...
回答by Emeka Obianom
How does it work? How does it know it's me?
它是如何工作的?它怎么知道是我?
Most sessions set a user-key(called the sessionid) on the user's computer that looks something like this: 765487cf34ert8dede5a562e4f3a7e12. Then, when a session is opened on another page, it scans the computer for a user-key and runs to the server to get your variables.
大多数会话在用户的计算机上设置一个用户密钥(称为 sessionid),看起来像这样:765487cf34ert8dede5a562e4f3a7e12。然后,当会话在另一个页面上打开时,它会扫描计算机以获取用户密钥并运行到服务器以获取您的变量。
If you mistakenly clear the cache, then your user-key will also be cleared. You won't be able to get your variables from the server any more since you don't know your id.
如果您错误地清除了缓存,那么您的用户密钥也将被清除。您将无法再从服务器获取变量,因为您不知道自己的 ID。
回答by Ali A. Dhillon
In my Ubuntu machine sessions are stored at
在我的 Ubuntu 机器中,会话存储在
/var/lib/php/sessions
and you have to sudo lsin this directory only lsit will throw
你必须sudo ls在这个目录中,只有ls它会抛出
ls: cannot open directory '.': Permission denied
ls: 无法打开目录 '.': 权限被拒绝
And on my Windows Wamp server php sessions are stored in
在我的 Windows Wamp 服务器上,php 会话存储在
C:\wamp64\tmp
and if you install standalone php on windows then there is no value set by default
如果您在 Windows 上安装了独立的 php,那么默认情况下没有设置任何值
session.save_path => no value => no value

