driesken
driesken

Reputation: 103

use session cookies per specific browsertab

I currently have a form that allows a user to edit entries. However, it doesn't seem to be possible to use 1 (specific) cookie per tab. Whenever a user edits an entry, the record in the last tab gets updated.

I've tried the following in my main script (eventfilters.php):

<?php
$cookie_name = $_SESSION['username'].md5(time());
session_name($cookie_name);
setcookie(session_name($cookie_name),session_id(),time()+"300");
if(!isset($_SESSION)){ session_start(); }

if (isset($_GET['edit'])){  
    // Pass cookiename in url variable $cookie, so it gets caught by $_GET['cookie']
    echo '<form action="eventfilters.php?save&cookie='.session_name().'" method="post">';

} else if (isset($_GET['save'])){
    if(isset($_GET['cookie'])){
        error_log("SAVE ".$_GET['cookie']); // Displays cookie url variable set by form action.
        error_log("LOW ".$_SESSION['level_low']); 
        // Displays correct session value received from ajax
    }
}

?>

`

The ajaxcode (also in eventfilters.php) contains this (called few times when page is already loaded):

$.post("include/severitygroups.php",{'cookie_name': "<?php echo $cookie_name; ?>", 'serialized_sev_groups': serialized_sev_groups}, function(data){});

This seems to pass the right cookiename to the other script, which successfully seems to return $_SESSION['level_low'] (as it appears in the error_log).

<?php
include('pdodb.php');

if(!isset($_SESSION)){ session_start(); }
$cookie_name = $_POST['cookie_name'];
error_log("SCRIPT ".$cookie_name);
error_log("COOKIEDATA ".$_COOKIE["$cookie_name"]);

// populating $_SESSION['level_low']
?>

It seems that the $_GET['save'] is populating the wrong sessions (initialised by the last loaded instance of eventfilters.php), even when the $_GET['save'] logs the right $_SESSION['level_low'] to the errorlog.

What is going wrong?

Upvotes: 0

Views: 144

Answers (1)

Ignacio Ocampo
Ignacio Ocampo

Reputation: 2713

You can't do it. The best way to do it, is with Ajax, so, you will pass an "ID" to each tab (or page) like:

editPost.php?id=someID

And, each time you press save, you should send your content with your id param to save it.

Upvotes: 1

Related Questions