PHP使用会话名称在会话之间切换

PHP使用会话名称在会话之间切换,php,session,Php,Session,因此,我试图在同一个脚本中编写两个php会话,但它似乎没有像我所想的那样工作 守则: // Start our first session with the name 's1' session_name('s1'); session_start(); $_SESSION['foo'] = 'Foo'; session_write_close(); // Start our second session with the name 's2' session_name('s2'); sessio

因此,我试图在同一个脚本中编写两个php会话,但它似乎没有像我所想的那样工作

守则:

// Start our first session with the name 's1'
session_name('s1');
session_start();

$_SESSION['foo'] = 'Foo';
session_write_close();

// Start our second session with the name 's2'
session_name('s2');
session_start();

$_SESSION['foo'] = 'Bar';
session_write_close();

// We now open our first session and print its value
session_name('s1');
session_start();

print_r($_SESSION['foo']);
它打印
Bar
我们认为它应该打印
Foo
,因为我们打开了会话
s1

问题: 正确的方法是什么(我认为使用session_id不是一种安全的方法)让它打印
Foo


另外,如果您编写类似于
$\u SESSION['color']='red'在会话
s2
中,您继续打印整个
s1
会话,您将在那里看到键
颜色,它不应该在那里


我是否误用了会话?

如果您使用会话id而不是会话名称,您的问题可以得到解决

// Start our first session with the name 's1'
session_name('s1');
session_start();

$_SESSION['foo'] = 'Foo';
session_write_close();

// Start our second session with the name 's2'
session_name('s2');
//session_start();

$_SESSION['foo'] = 'Bar';
session_write_close();

// We now open our first session and print its value
session_name('s1');
session_start();

print_r($_SESSION['foo']);
将此等式改为

<?php
   // Start our first session with the name 's1'

session_id('s1');
session_start();

$_SESSION['foo'] = 'Foo';
session_write_close();

// Start our second session with the name 's2'

session_id('s2');
session_start();

$_SESSION['foo'] = 'Bar';
session_write_close();

// We now open our first session and print its value
session_id('s1');

session_start();
print_r($_SESSION['foo']);
?>

会话id是会话的标识符,因此不同会话的id可能不同,但会话名称只是同一会话的不同名称。

Session\u name()
设置发送到浏览器的cookie名称,如图所示。

php为会话使用标识符,其中包含图片中cookie的值。 可通过@Amit Ray指示的
会话\u id
进行更改

下面是另一个棘手的解决方案,您可以动态更改
会话.path
。。您将在两个不同的文件夹中有两个具有相同标识符的文件

这是密码

<?php

session_save_path('/tmp/phpsess1/');
session_start();

$_SESSION['foo'] = 'Foo';
session_write_close();


// Start our second session with the name 's2'

session_save_path('/tmp/phpsess2');
session_start();
$_SESSION['foo'] = 'Bar';
session_write_close();
//
// // We now open our first session and print its value


session_save_path('/tmp/phpsess1');
session_start();
print_r($_SESSION);

有一种方法可以使用会话名称(cookie名称)在会话之间切换。我正在开发一个需要此功能的web应用程序,并提出了以下代码

/* set the first session name */
session_name($ses->one->name);
/* check if the first session cookie is set and use the value as the id */
if(isset($_COOKIE[$ses->one->name])) session_id($_COOKIE[$ses->one->name]);
/* start the session */
session_start();
/* store data in this session */
$_SESSION['name'] = $ses->one->name;
/* get the session id */
$ses->one->id = session_id();
/* close the session */
session_write_close();

/* repeat the same process for the second session */
session_name($ses->two->name);
/* if you loading the sessions as the same time you will need to assign a different session id to the second session */
if(isset($_COOKIE[$ses->two->name])) session_id($_COOKIE[$ses->two->name]);
else session_id($ses->one->id . '_1');
session_start();
$_SESSION['name'] = $ses->two->name;
$ses->two->id = session_id();
session_write_close();
现在是检索存储的数据的时候了

/* set the session name */
session_name($ses->one->name);
/* set the session id */
session_id($ses->one->id);
/* start the session */
session_start();
/* echo out the data */
echo 'session 1 : ' . $_SESSION['name'] . '<br>';
/* close the session */
session_write_close();

/* repeat the same process for the second session */
session_name($ses->two->name);
session_id($ses->two->id);
session_start();
echo 'session 2 : ' . $_SESSION['name'];
session_write_close();
/*设置会话名称*/
会话名称($ses->one->name);
/*设置会话id*/
会话id($ses->one->id);
/*开始会话*/
会话_start();
/*呼出数据*/
回显“会话1:”$_会话['name']。'
'; /*结束会议*/ 会话写入关闭(); /*对第二个会话重复相同的过程*/ 会话名称($ses->two->name); 会话id($ses->two->id); 会话_start(); 回显“会话2:”$_会话['name']; 会话写入关闭();

我希望这对你们中的一些人有所帮助。

在每个阶段都执行
var\u dump($\u SESSION)
。现在没有访问php的权限,但我猜它只是将加载的会话与调用session_start()时$_会话中的任何内容合并在一起,因此您在以前/不同命名会话中所做的所有会话工作都会累积到$_session中。@MarcB即使执行
$_session=[]每次
会话后写入关闭()它也一样。那么,如果不更改会话的id,就没有办法做到这一点?我每隔一次操作都会重新生成id,所以这对我来说可能是个问题。@VictorTello,如果您要生成id,请不要使用像s1或s2这样简单的东西。。因为Cookie注入攻击似乎很有趣,谢谢,但是,为了澄清,没有办法用会话名称来实现这一点,对吗?@VictorTello不,你必须使用
session\u id
或上面这个棘手的方法。但实际上我推荐
session\u id
,因为它更简单。是的,我在使用hhvm的session\u save\u路径时遇到问题。。。我将使用会话id。。。谢谢:D