简体   繁体   English

一个Web应用程序上的多个会话

[英]multiple sessions on one web application

I know I can use $oldSession = session_name("mySessName"); 我知道我可以使用$oldSession = session_name("mySessName"); to set the name of the session, which I do like so: 设置会话的名称,我喜欢这样:

# FileName: sessionTest.php
$old_name = session_name("TEST");
session_start();
$_SESSION["hi"]="hi";
print_r($_SESSION);

I can even have another file: sessionTest1.php which contains the following: 我甚至可以有另一个文件:sessionTest1.php,其中包含以下内容:

# FileName: sessionTest.php
$old_name = session_name("TEST1");
session_start();
$_SESSION["Bar"]="bar";
print_r($_SESSION);

I can go back and forth between sessionTest.php and sessionTest1.php and the session will only have the corresponding variable. 我可以在sessionTest.php和sessionTest1.php之间来回切换,会话只会有相应的变量。

The issue I am running into is suppose a different script already has a session started and then calls this file. 我遇到的问题是假设一个不同的脚本已经启动会话,然后调用此文件。 What I am seeing is suppose I have: 我所看到的是假设我有:

session_name("other");
session_start();
$_SESSION["foo"] = "foo";
require_once "sessionTest.php";
print_r($_SESSION);

This is printing Array( "foo" => "foo", "hi" => "hi" ) . 这是打印Array( "foo" => "foo", "hi" => "hi" ) Is there a way to end the previous session and start my session fresh. 有没有办法结束上一个会话并重新开始我的会话。 Note: I don't want to destroy the previous session as there may be valuable information in it. 注意:我不想破坏之前的会话,因为其中可能包含有价值的信息。

what i do is make my SESSION 1 layer deeper then the standard. 我所做的是让我的SESSION比标准更深一层。 so i can just use that layer of the array. 所以我可以使用该阵列的那一层。

some page: 一些页面:

 <?php 
       $_SESSION['myApp1']['hi'] = "Hi";
  ?>

some other page: 其他一些页面:

 <?php
      $_SESSION['myApp2']['ciao'] = "Ciao";
 ?>

so when i want to see session vars on page 2 i just 所以当我想在第2页看到会话变量时我就是这样

<?php
     echo "<pre>";
     print_r($_SESSION['myApp2']);
     echo "</pre>";

 ?>

use session_name before session_start. 在session_start之前使用session_name。

PHP session_name PHP session_name

The session name is reset to the default value stored in session.name at request startup time. 在请求启动时,会话名称将重置为存储在session.name中的默认值。 Thus, you need to call session_name() for every request (and before session_start() or session_register() are called). 因此,您需要为每个请求调用session_name()(并在调用session_start()或session_register()之前)。

read this SO answer: 阅读这个SO答案:

Multiple Sessions 多个会话

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

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