RazorFinger RazorFinger - 3 months ago 18
PHP Question

How to have different sessions in different tabs

I need to have a session for each tab that is opened by the user. Some would say that this is impossible, because sessions are stored in cookies, and cookies don't recognize tabs and all that.

But there was a comment made on the PHP manual that made me think maybe it was possible, i'm just not sure how to do the second part. But let's start from the beginning.

So, the idea is having different Session_names based on a unique ID, then passing all that through the URL. So it is something like this:

if(!preg_match('/^SESS[0-9]+$/',$_REQUEST['SESSION_NAME'])) {
$_REQUEST['SESSION_NAME']='SESS'.uniqid('');
}
output_add_rewrite_var('SESSION_NAME',$_REQUEST['SESSION_NAME']);
session_name($_REQUEST['SESSION_NAME']);


So, so far so good, i am generating the unique ID. The problem is, how do i pass this ID onto my tabs? through the URL? Even so, how would i go about inserting it into my URL? The commenter specified something like this:

< ?php
header('location: script.php?'.session_name().'='.session_id()
. '&SESSION_NAME='.session_name());
?>

<input type="image" src="button.gif" onClick="javascript:open_popup('script.php?<?php
echo session_name(); ?>=<?php echo session_id(); ?>&SESSION_NAME=<?php echo session_name(); ?>')" />


But i'm not sure how to implement this onto my own website. I need to pass it onto this page (that i'm opening through js)

document.getElementById("details").action = "../details.php?tipo=" + tipo + "&periodo=" + periodo;


So how do i add the session information to this URL?

EDIT: I need to have a different session per tab, because every tab can export it's information onto another page which would then export to a excel file. That part works fine, but if the user opens two pages at the same time, only the latest one will be passed through the session.

Answer

your approach is a little bit too complex for this task.

Instead of messing with variables, trying to store random stuff rewriting vars, you can just store nested arrays in session variables.

For example, on a page where user starts things out, do something like

$_SESSION['<random_string>']=array();

and then have the value of this random string stored in one of the 'hidden' input fields.

Whenever a form is submitted, or you need to do any kind of operation with session variables,do

if($_POST['action']=='Buy_bananas')
{
if(isset($_SESSION['<string>']))
{
buy($_SESSION['<string>']['bananas_to_buy']);
}
else
echo "Stop breaking code";
}
Comments