PHP 多个会话
PHP multiple sessions
为什么我在打印时只得到上次会话的输出?我需要为每个用户 ID 保存一个会话并发送密码重置电子邮件。当用户单击 link 并更改密码时,我需要从服务器清除会话。
这就是我用 PHP 做的。
$res = array();
$uniqueId = uniqid();
echo $uniqueId . "<br>";
session_id($uniqueId);
session_start();
echo session_id() . "<br>";
$_SESSION['session_id'] = session_id();
$_SESSION['event_id'] = 'event1';
$_SESSION['user_id'] = 'user1';
$res[] = json_encode($_SESSION);
$uniqueId2 = uniqid();
echo $uniqueId2 . "<br>";
session_destroy();
session_id($uniqueId2);
session_start();
echo session_id() . "<br>";
$_SESSION['session_id'] = session_id();
$_SESSION['event_id'] = 'event2';
$_SESSION['user_id'] = 'user2';
$res[] = json_encode($_SESSION);
echo "<br>";
print_r($res);
print_r
的输出:
Array (
[0] => {"session_id":"5609187f586da","event_id":"event1","user_id":"user1"}
[1] => {"session_id":"5609187f588e1","event_id":"event2","user_id":"user2"}
)
现在在一个新页面中,当我像这样尝试每个会话的事件 ID 时,我只得到最后一个会话的 event_id 而不是两者。首先它说
Notice: Undefined index: event_id in C:\xampp\htdocs\test\test.php on line 12
这就是我在新页面中所做的。
$id1 = '560915a8c0875';
$id2 = '560915a8c0d51';
session_id($id1);
session_start();
echo $_SESSION['event_id'];
echo "<br>";
session_id($id2);
echo $_SESSION['event_id'];
这对 PHP 或什么是不可能的?
session_destroy() destroys all of the data associated with the current session. It does not unset any of the global variables associated with the session, or unset the session cookie. To use the session variables again, session_start() has to be called.
http://php.net/manual/en/function.session-destroy.php
当在 $id1 上调用 session_destroy 时,数据也将从服务器中清除,这意味着当您将会话 ID 定义为 $id1 时,它将 return 一个空会话。
为什么我在打印时只得到上次会话的输出?我需要为每个用户 ID 保存一个会话并发送密码重置电子邮件。当用户单击 link 并更改密码时,我需要从服务器清除会话。
这就是我用 PHP 做的。
$res = array();
$uniqueId = uniqid();
echo $uniqueId . "<br>";
session_id($uniqueId);
session_start();
echo session_id() . "<br>";
$_SESSION['session_id'] = session_id();
$_SESSION['event_id'] = 'event1';
$_SESSION['user_id'] = 'user1';
$res[] = json_encode($_SESSION);
$uniqueId2 = uniqid();
echo $uniqueId2 . "<br>";
session_destroy();
session_id($uniqueId2);
session_start();
echo session_id() . "<br>";
$_SESSION['session_id'] = session_id();
$_SESSION['event_id'] = 'event2';
$_SESSION['user_id'] = 'user2';
$res[] = json_encode($_SESSION);
echo "<br>";
print_r($res);
print_r
的输出:
Array (
[0] => {"session_id":"5609187f586da","event_id":"event1","user_id":"user1"}
[1] => {"session_id":"5609187f588e1","event_id":"event2","user_id":"user2"}
)
现在在一个新页面中,当我像这样尝试每个会话的事件 ID 时,我只得到最后一个会话的 event_id 而不是两者。首先它说
Notice: Undefined index: event_id in C:\xampp\htdocs\test\test.php on line 12
这就是我在新页面中所做的。
$id1 = '560915a8c0875';
$id2 = '560915a8c0d51';
session_id($id1);
session_start();
echo $_SESSION['event_id'];
echo "<br>";
session_id($id2);
echo $_SESSION['event_id'];
这对 PHP 或什么是不可能的?
session_destroy() destroys all of the data associated with the current session. It does not unset any of the global variables associated with the session, or unset the session cookie. To use the session variables again, session_start() has to be called. http://php.net/manual/en/function.session-destroy.php
当在 $id1 上调用 session_destroy 时,数据也将从服务器中清除,这意味着当您将会话 ID 定义为 $id1 时,它将 return 一个空会话。