PHP 如何创建多个会话?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24964699/
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
PHP How can I create multiple sessions?
提问by Nathan
I want to be able to switch back and forth between sessions in php. Here is my current code:
我希望能够在 php 中的会话之间来回切换。这是我当前的代码:
<?php
session_name("session1");
session_start();
$_SESSION["name"] = "1";
echo "<pre>", print_r($_SESSION, 1), "</pre>";
session_write_close();
session_name("session2");
session_start();
$_SESSION["name"] = "2";
echo "<pre>", print_r($_SESSION, 1), "</pre>";
session_write_close();
session_name("session1");
session_start();
echo "<pre>", print_r($_SESSION, 1), "</pre>";
I want it to output
我想要它输出
Array
(
[name] => 1
)
Array
(
[name] => 2
)
Array
(
[name] => 1
)
but it is outputting
但它正在输出
Array
(
[name] => 1
)
Array
(
[name] => 2
)
Array
(
[name] => 2
)
Is it possible to switch between sessions like that? I don't need two sessions running at the same time, but I do need to be able to switch between them. When I run this code, I get two cookies: session1 and session2 with the same value.
是否可以在这样的会话之间切换?我不需要同时运行两个会话,但我确实需要能够在它们之间切换。当我运行此代码时,我得到两个具有相同值的 cookie:session1 和 session2。
Thanks for any help!
谢谢你的帮助!
回答by raidenace
What you need to use is session_id()
instead of session_name()
你需要使用的是session_id()
而不是session_name()
<?php
session_id("session1");
session_start();
echo session_id();
$_SESSION["name"] = "1";
echo "<pre>", print_r($_SESSION, 1), "</pre>";
session_write_close();
session_id("session2");
echo session_id();
session_start();
$_SESSION["name"] = "2";
echo "<pre>", print_r($_SESSION, 1), "</pre>";
session_write_close();
session_id("session1");
echo session_id();
session_start();
echo "<pre>", print_r($_SESSION, 1), "</pre>";
session_write_close();
session_id("session2");
echo session_id();
session_start();
echo "<pre>", print_r($_SESSION, 1), "</pre>";
This will print:
这将打印:
session1
Array
(
[name] => 1
)
session2
Array
(
[name] => 2
)
session1
Array
(
[name] => 1
)
session2
Array
(
[name] => 2
)
session_id
is an identifier for a session, which helps in distinguishing sessions. session_name
is only a named alias for the current session
session_id
是会话的标识符,有助于区分会话。session_name
只是当前会话的命名别名
回答by humanityANDpeace
As the comments to the existing answerindicate, the offered solution might not be ideal and I would like to provide some alternative. Let it be a function named sane_session_name()
, which looks like this:
正如对现有答案的评论所表明的那样,提供的解决方案可能并不理想,我想提供一些替代方案。让它成为一个名为 的函数sane_session_name()
,它看起来像这样:
function sane_session_name($name)
{
session_name($name);
if(!isset($_COOKIE[$name]))
{
$_COOKIE[$name] = session_create_id();
}
session_id($_COOKIE[$name]);
}
By using the "sane" subsitution for session_name()
in the OP's original code, we get this:
通过session_name()
在 OP 的原始代码中使用“理智”替代,我们得到:
<?php
sane_session_name("session1");
session_start();
$_SESSION["name"] = "1";
echo "<pre>", print_r($_SESSION, 1), "</pre>";
session_write_close();
sane_session_name("session2");
session_start();
$_SESSION["name"] = "2";
echo "<pre>", print_r($_SESSION, 1), "</pre>";
session_write_close();
sane_session_name("session1");
session_start();
echo "<pre>", print_r($_SESSION, 1), "</pre>";
and it will yield the desired output:
它将产生所需的输出:
Array
(
[name] => 1
)
Array
(
[name] => 2
)
Array
(
[name] => 1
)
What is different?
有什么不同吗?
To point out the difference between this answer and the raidenace's answer:
指出这个答案和raidenace的答案之间的区别:
- In raidenace's answertwo sessions are created for all clients shared among all visitor of the website.
- With this answer two sessions are created for each visitor to the website. Consequently this would allow that in the
$_SESSION
superglobal different content can be stored for visitor Alice and Bob, while in the other two website visitor Alice an Bob would "share the data", and rather pointlessly a cookie namedPHPSESSID
with the value session2 is set each time and send back and forth.
- 在raidenace 的回答中,为在网站所有访问者之间共享的所有客户端创建了两个会话。
- 有了这个答案,就会为网站的每个访问者创建两个会话。因此,这将允许在
$_SESSION
超全局中可以为访问者 Alice 和 Bob 存储不同的内容,而在其他两个网站访问者 Alice 中,Bob 将“共享数据”,并且PHPSESSID
每次都设置一个名为session2的 cookie 毫无意义并来回发送。
Security
安全
To protect those "multiple (per user) sessions" from session fixationand session hiHymaning, we can further use this litte function
为了保护那些“多个(每个用户)会话”免受会话固定和会话劫持,我们可以进一步使用这个小功能
function sane_session_start($name)
{
ini_set("session.use_strict_mode",true);
ini_set("session.cookie_httponly",true);
session_name($name);
if(!isset($_COOKIE[$name]))
{
$_COOKIE[$name] = session_create_id();
}
session_id($_COOKIE[$name]);
session_start();
session_regenerate_id(true);
$_COOKIE[$name] = session_id();
}
and have the OP's code look like this:
并让 OP 的代码如下所示:
<?php
sane_session_start("session1");
$_SESSION["name"] = "1";
echo "<pre>", print_r($_SESSION, 1), "</pre>";
session_write_close();
sane_session_start("session2");
$_SESSION["name"] = "2";
echo "<pre>", print_r($_SESSION, 1), "</pre>";
session_write_close();
sane_session_start("session1");
echo "<pre>", print_r($_SESSION, 1), "</pre>";