Question

I know I can use $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:

# 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.

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" ). 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.

No correct solution

OTHER TIPS

what i do is make my SESSION 1 layer deeper then the standard. 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

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

 ?>

use session_name before session_start.

PHP session_name

The session name is reset to the default value stored in session.name at request startup time. Thus, you need to call session_name() for every request (and before session_start() or session_register() are called).

read this SO answer:

Multiple Sessions

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top