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...
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:
Finally - putting it all together into the function you wanted :
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.