php 即使在浏览器关闭后也可以保持会话吗?

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

Is possible to keep session even after the browser is closed?

phpsession

提问by Octavian

Could anyone tell how to maintain a session (in PHP) so that the session contains are preserved and are accessible even after the browser is restarted.

任何人都可以告诉如何维护会话(在 PHP 中),以便即使在浏览器重新启动后,会话所包含的内容也能被保留和访问。

In general a session expires with the closing of a browser, but I want the session NOT TO BE CLOSED so that the session data's can be accessed the next time the browser is used.

通常,会话会随着浏览器的关闭而过期,但我希望会话不会被关闭,以便下次使用浏览器时可以访问会话数据。

回答by Ignacio Vazquez-Abrams

Use session_set_cookie_parameters()to give the session cookie a non-zero lifetime before starting the session, or set session.cookie_lifetimeto non-zero.

用于session_set_cookie_parameters()在开始会话之前为会话 cookie 提供非零生命周期,或设置session.cookie_lifetime为非零。

回答by Your Common Sense

It's oxymoron.
Session stands for "until browser is closed".
Session is something that expires.
If you don't want it to be expired, you're probably don't want a session at all.

这是矛盾的。
会话代表“直到浏览器关闭”。
会话是过期的东西
如果您不希望它过期,那么您可能根本不需要会话。

You are probably messing session with cookie or database.

您可能正在使用 cookie 或数据库搞乱会话。

回答by John Max

The easiest and best i have found is that instead of just session_start we should input this on each page there is a session

我发现的最简单和最好的方法是,我们应该在每个页面上输入它而不是 session_start,而是有一个会话

$expire = 365*24*3600; // We choose a one year duration

ini_set('session.gc_maxlifetime', $expire);

session_start(); //We start the session 

setcookie(session_name(),session_id(),time()+$expire); 
//Set a session cookies to the one year duration

回答by remi bourgarel

Session in php (and in most web technologies) work like this :

php(以及大多数 Web 技术)中的会话是这样工作的:

You store a session id in a cookie on the client computer.

您将会话 ID 存储在客户端计算机上的 cookie 中。

When the client come to your site he send you the session id.

当客户访问您的站点时,他会向您发送会话 ID。

The server find the session datas in a file with the session id and load it.

服务器在具有会话 id 的文件中查找会话数据并加载它。

So closing the browser has not effect on the session, but if the browser empty the cookie when you close it (I don't think any browser do such a thing).

所以关闭浏览器对会话没有影响,但如果浏览器在关闭时清空 cookie(我认为没有任何浏览器会做这样的事情)。

If you wana be sure the user is always logged in, you can store it's user/password in his cookies but it's not really safe.

如果您想确保用户始终登录,您可以将其用户/密码存储在他的 cookie 中,但这并不安全。

回答by u2603230

You can do something like this: (see session_set_cookie_parameters()and session_name())

你可以做这样的事情:(见session_set_cookie_parameters()session_name()

// long long time
$sessionTime = 365 * 24 * 60 * 60;
$sessionName = "my_session";
session_set_cookie_params($sessionTime);
session_name($sessionName);
session_start();

if (isset($_COOKIE[$sessionName])) {
    setcookie($sessionName, $_COOKIE[$sessionName], time() + $sessionTime, "/");
}

For $sessionTime, also refer to this question

对于$sessionTime,另请参阅此问题

回答by Mischa

This can be done if you use cookies instead of sessions.

如果您使用 cookie 而不是会话,则可以完成此操作。