简体   繁体   English

PHP如何创建多个会话?

[英]PHP How can I create multiple sessions?

I want to be able to switch back and forth between sessions in php. 我希望能够在php之间的会话之间来回切换。 Here is my current code: 这是我目前的代码:

<?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! 谢谢你的帮助!

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_id是会话的标识符,有助于区分会话。 session_name is only a named alias for the current session session_name只是当前会话的命名别名

As the comments to the existing answer indicate, 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: 通过在OP的原始代码中使用session_name()的“理智”替代,我们得到:

<?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 answer two sessions are created for all clients shared among all visitor of the website. raidenace的回答中 ,为网站所有访问者共享的所有客户创建了两个会话。
  • 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 named PHPSESSID with the value session2 is set each time and send back and forth. 因此,这将允许在$_SESSION超全局中为访问者Alice和Bob存储不同的内容,而在另外两个网站访问者Alice和Bob将“共享数据”,而毫无意义地,名为PHPSESSID的cookie值为session2每次设置并来回发送。

Security 安全

To protect those "multiple (per user) sessions" from session fixation and session hijacking , we can further use this litte function 为了保护那些“多个(每个用户)会话”免受会话固定会话劫持的影响 ,我们可以进一步使用这个litte函数

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>";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM