如何还原一个PHP会话? [英] How to restore a PHP session?

查看:235
本文介绍了如何还原一个PHP会话?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我所知,PHP存储用户的会话ID在一个名为PHPSESSID的cookie存储在客户端浏览器上,并与服务器上的会话匹配的是能够涉及的2.关闭浏览器后,
会话信息自败,但在客户端上的cookie仍然存在。是否有可能使用这个cookie来恢复旧的会话?还是所有的会话数据从服务器中删除的那一刻客户关闭浏览器?

I understand that PHP stores a user's session id in a cookie called "PHPSESSID" which is stored in the client's browser and is matched against the session on the server to be able to relate the 2. After closing the browser the session info dissapears but the cookie on the client remains. Is it possible to use this cookie to restore the old session? Or does all the session data get deleted from the server the moment the client closes their browser?

我第一次有这种我的网页上:

I had this on my page first:

session_start();
$_SESSION['message'] = 'Hello';

echo $_SESSION['message']; // outputs hello

然后我改变了页面为:

then I changed the page to:

$old_session = session_id();
session_id($old_session);
session_start();

echo $_SESSION['message'];

然后我关闭了浏览器,重新打开这个页面,得到了这些错误:

Then I closed the browser and reopened it to this page and got these errors:

Warning: session_start() [function.session-start]: The session id is too long or contains illegal characters, valid characters are a-z, A-Z, 0-9 and '-,' in C:\xampp\htdocs\localhost\test.php on line 5

Notice: Undefined index: message in C:\xampp\htdocs\localhost\test.php on line 7

究竟怎样才能在关闭浏览器后恢复旧的会话信息,它甚至有可能?

How exactly does one retrieve old session info after closing the browser, is it even possible?

推荐答案

一个会话不正是它在锡说 - 存在客户端的会话的持续时间。当你关闭浏览器的定义(如有)浏览会话结束。

A session does exactly what it says on the tin - exists for the duration of the client's session. A browsing session by definition (such as there is one) ends when you close the browser.

基于Cookie的会话通过设置在PHP定义为 0 一生的cookie的工作 - 这意味着浏览器应该销毁该cookie时,关闭浏览器。一旦饼干已被破坏,会话ID没有在任何后续的服务器请求发送,因此会话数据不会在你的PHP脚本可用。

Cookie-based sessions work by setting a cookie that has a lifetime defined in PHP as 0 - this means that the browser should destroy the cookie when the browser is closed. Once the cookie has been destroyed, the session ID is not sent in any subsequent server requests, so the session data will not be available in your PHP script.

然而,会话数据没有在在用户关闭浏览器的时刻在服务器端被破坏,如你所说 - 这是不可能的,因为客户端不通知它已被关闭的服务器。相反,在服务器端的会话数据具有其具有15分钟的默认值的TTL(时间到活)。在此之后已经到期,则数据的可能的可在由会话垃圾收集器的任何时间删除。在理论上,这可能是相当长的时间,但在一个繁忙的服务器上实践的数据将一对夫妇中的TTL即将过期的分钟内被删除。

However, the session data is not destroyed at the server side at the moment the user closes the browser, as you suggested - this is impossible, because the client does not notify the server that it has been closed. Instead, the session data at the server side has a TTL (time-to-live) which has a default value of 15 minutes. After this has expired, the data may be deleted at any time by the session garbage collector. In theory this could be some considerable time, but in practice on a busy server the data will be deleted within a couple of minutes of the TTL expiring.

然而,PHP不能使会话数据可用,除非它具有会话ID,并且它不会有会话ID如果cookie已被破坏,这是我说的,应该当用户关闭浏览器发生。

However, PHP cannot make the session data available unless it has the session ID, and it will not have the session ID if the cookie has been destroyed, which as I say, should happen when the user closes their browser.

所以短期问题的答案如何恢复一个PHP会话是:你不能

So the short answer to the question How can I restore a PHP session? is: You can't

这篇关于如何还原一个PHP会话?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆