如何在 PHP 中创建持久会话?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9797913/
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-26 07:38:26  来源:igfitidea点击:

How do I create persistent sessions in PHP?

session-cookiesphp

提问by John Hoffman

I used session_start()to initiate a session in PHP, but when my browser closes, the session is gone.

我曾经session_start()在 PHP 中启动一个会话,但是当我的浏览器关闭时,会话就消失了。

How do I use PHP to create persistent sessions that last across browser closes?

如何使用 PHP 创建跨浏览器关闭的持久会话?

回答by drew010

See the php.inivalue session.cookie_lifetime.

请参阅php.inisession.cookie_lifetime

The default value of 0means to end the session when the browser closes.

默认值0意味着在浏览器关闭时结束会话。

You can override this value either directly in php.inior set it in your application prior to starting the session using ini_set. Setting it to something greater than 0will cause the session to live for that duration.

php.ini使用ini_set启动会话之前,您可以直接在应用程序中覆盖此值或在应用程序中设置它。将其设置为大于0将导致会话在该持续时间内存活的值。

E.g.

例如

ini_set('session.cookie_lifetime', 60 * 60 * 24 * 7);  // 7 day cookie lifetime
session_start();

The above example causes the session cookie to be set with an expiration of 7 days from when the session is started.

上面的示例导致会话 cookie 设置为从会话开始后 7 天到期。

Note:If you start your session for all of your webpages from the same piece of code, this will not continue to extend the session expiration each time session_start()gets called. The cookie lifetime is set from when the session is first started, not on subsequent requests. If you want to extend the lifetime of a session out 7 days from the current time, see also session_regenerate_id().

注意:如果您从同一段代码为所有网页启动会话,则每次session_start()调用时都不会继续延长会话到期时间。cookie 生存期是从会话第一次启动时设置的,而不是在后续请求中设置。如果您想将会话的生命周期从当前时间延长 7 天,另请参阅session_regenerate_id()

Also Note:If your session.gc_maxlifetimevalue is set to something less than the length of the session cookie, you can have a situation where the user does not visit the site for 5 days and when they return, the session cookie is no longer valid because the data on the server has been deleted. To remedy this, you should also set the lifetime for this session data to be at least as long as your cookie lifetime. As the manual states, it may be necessary to use a custom session.save_pathfor the session data you want to persist longer than the default. Therefore, your script may look like this:

另请注意:如果您的session.gc_maxlifetime值设置为小于会话 cookie 的长度,您可能会遇到这样的情况:用户 5 天未访问该站点,当他们返回时,会话 cookie 不再有效因为服务器上的数据已经被删除了。为了解决这个问题,您还应该将此会话数据的生命周期设置为至少与您的 cookie 生命周期一样长。正如手册所述,可能有必要为session.save_path要保留的会话数据使用自定义的比默认值更长的时间。因此,您的脚本可能如下所示:

ini_set('session.cookie_lifetime', 60 * 60 * 24 * 7);
ini_set('session.gc_maxlifetime', 60 * 60 * 24 * 7);
ini_set('session.save_path', '/home/yoursite/sessions');
session_start();

回答by Rohit Chopra

Sessions are designed such that they are "Session" based. In other words, if you close your Browser (which is essentially your session), it is suppose to go away.

会话被设计为基于“会话”。换句话说,如果您关闭浏览器(本质上是您的会话),它就会消失。

You could try storing the session data in a database instead of in a file. Store the session Id and the session data in a table. Then recall the session ID from the cookie PHPSESSID (by default) and look up the session data from your database.

您可以尝试将会话数据存储在数据库中而不是文件中。将会话 ID 和会话数据存储在一个表中。然后从 cookie PHPSESSID(默认情况下)中调用会话 ID,并从您的数据库中查找会话数据。

回答by Robert

I would recommend using cookies and a database if you want a persistent session. We store the customer's ID (random 32 bit alph-anumeric value) in a cookie and then reference that to load up their customer information.

如果您想要持久会话,我建议使用 cookie 和数据库。我们将客户的 ID(随机的 32 位字母数字值)存储在 cookie 中,然后参考它来加载他们的客户信息。

回答by MuhammadAfif

You can start your session without typing session_start()and you can start it with cookie like this

您无需输入即可开始会话,session_start()并且可以像这样使用 cookie 启动它

setcookie('PHPSESSID','any id' , any time);

this could be done because when you type session_start()and then try to look the cookie like this

这样做是可以的,因为当您键入session_start()然后尝试像这样查看 cookie 时

print_r($_COOKIE);

then the outpout will be :

那么输出将是:

Array ( [PHPSESSID] => c0voj7h0b4aesddkc17a6jk7c3 )

just try it yourself

自己试试吧