Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Switch between sessions

Tags:

php

session

Is there a way to switch between sessions in php?

I am storing a lot of data in php sessions and having many overflow issues, so now the first solution that came is subdivide session data somehow. Example:

//Uses session sector 1

switch_to_session('sector1');
$_SESSION['data1'] = 'tons of data'; //store data

//Uses session sector 2

switch_to_session('sector2');
$_SESSION['data1'] = 'another data';

//Return to sector 1
switch_to_session('sector1');
echo $_SESSION['data1']; //prints: 'tons of data'

Is that possible? Thanks in advance...

like image 955
Carlos Avatar asked Dec 16 '22 00:12

Carlos


2 Answers

Although I suspect there is a better way of doing whatever it is that you are trying to do - in strict answer to your question : yes - you can switch sessions.

The trick is to save and close your existing session, then identify your new session and then start it.

Example:

<?php
  session_start(); // start your first session
  echo "My session ID is :".session_id();

  $sess_id_1 = session_id(); // this is your current session ID
  $sess_id_2 = $sess_id_1."_2"; // create a second session ID - you need this to identify the second session. NOTE : *must be **unique** *;

  $_SESSION['somevar'] = "I am in session 1";
  session_write_close(); // this closes and saves the data in session 1

  session_id($sess_id_2); // identify that you want to go into the other session - this *must* come before the session_start
  session_start(); // this will start your second session
  echo "My session ID is :".session_id(); // this will be the session ID that you created (by appending the _2 onto the end of the original session ID

  $_SESSION['somevar'] = "I am in session 2";
  session_write_close(); // this closes and saves the data in session 2

  session_id($sess_id_1); // heading back into session 1 by identifying the session I you want to use
  session_start();

  echo $_SESSION['somevar']; //will say "I am in session 1";
?>

Finally - putting it all together into the function you wanted :

<?php 
  function switch_to_session($session_id) {
      if (isset($_SESSION)) {  // if there is already a session running
          session_write_close(); // save and close it
      }

      session_id($session_id); // set the session ID
      session_start();
  }
?>

That should do the trick.

Note : it is vital that your session IDs that are unique. If you do not, valuable user data is at risk.

To make life more complicated, you can also change your session handler (the way the session data is being stored) for each session that you switch to. If you are interfaceing with 3rd party code or systems, you may find that it is using a different session handler, and that can confuse matters. In this case you can also get/ set your session save handler and change that before starting the next session.

like image 185
xenmic Avatar answered Jan 02 '23 11:01

xenmic


Not a direct answer to your question..

You are going about this all wrong, if you need to store that much data then you need to be using a different storage method - preferably a database, file or cache store.

In the session itself you should store the reference to the data - A file name, DB primary key or cache key.

AFAIK you cant 'switch' sessions.

like image 41
SamV Avatar answered Jan 02 '23 12:01

SamV