4

Well, let's say I have a php-script, that has some select box, and I want to store the selected value in session array. Till now I assigned the value to my session array using the following construction:

$_SESSION[SCRIPT_NAME]['filter_data']

SCRIPT_NAME is a constant where I put script name, this is to make session variable unique per every php-script. Everything is fine till the moment when user opens the same script in two or more tabs to work simultaneously. In this case variables mixes and things go wrong...

May be you have any ideas how to make session variable absolutely unique per every script instance? Or any advice how to achieve functionality different way?

I now, I can pass my variables in URL, but I don't like this option. I want to use session and I want every script of my web-application remember users' choices during the whole user login session

6
  • 4
    Filters and such belong in the URL. If they're not there, how can I bookmark a result or send a link to another person? Commented Dec 30, 2012 at 18:32
  • It depends on what you're using the session storage for. You should first and foremost respect the idempotence of HTTP requests. If it can be reasonably transported in the URL, it should. There should be no hidden state. Commented Dec 30, 2012 at 18:35
  • Well Matti, this is a drawback of my approach. But since it is not a public web-site, it is internal web-application for organisation, more important is to remember filters while working. Commented Dec 30, 2012 at 18:38
  • I agree with the other comments here - put the filter in the URL, not in the session. Then you will not be fighting the issue with multiple browsers returning the session cookie. Commented Dec 30, 2012 at 18:38
  • 2
    @Georgiy: "It's an internal application" is no excuse to make it annoying to use. People use the web all the time and will expect web sites/application to behave in a certain way, "internal" or not. Commented Dec 30, 2012 at 18:41

6 Answers 6

3

"...how to make session variable absolutely unique per every script instance?"

Basically the answer is, "You can't." This is because all instances of the browser (windows and tabs) share the same cookie jar. So both instances return the same cookie and they are connected to the same session. It's rarely a problem in "real life" but it drives developers nuts, as you have now seen ;-)

Sign up to request clarification or add additional context in comments.

1 Comment

You can do this, check my answer
0
 <?php
        //Start session
        session_start();
        //Check whether the session variable SESS_MEMBER_ID is present or not
        if (!isset($_SESSION['id']) || ($_SESSION['id'] == '')) {
            header("location: index.php");
            exit();
        }

        $session_id=$_SESSION['id'];
        ?>

and file connection

 <?php

 class DbConnector {

 var $theQuery;
 var $link;

 function DbConnector(){

    // Get the main settings from the array we just loaded
    $host = 'localhost';
    $db = '';
    $user = 'root';
    $pass = '';

    // Connect to the database
    $this->link = mysql_connect($host, $user, $pass);
    mysql_select_db($db);
    register_shutdown_function(array(&$this, 'close'));

}

   //*** Function: query, Purpose: Execute a database query ***
function query($query) {

    $this->theQuery = $query;
    return mysql_query($query, $this->link);

}

//*** Function: fetchArray, Purpose: Get array of query results ***
function fetchArray($result) {

    return mysql_fetch_array($result);

}

//*** Function: close, Purpose: Close the connection ***
function close() {

    mysql_close($this->link);

}

}

?>

1 Comment

Please note that code only answers are discouraged. Better have a bit of textual explanations around!
0
  • generate a unique id for every page opened
  • pass that unique id to the script via get method
  • use that unique id instead of the script's name

problems with this approach

using this approach the session variables stored may take up very very much space over time if you don't clear the unused ones and the user opens your site a lot before the session expires

solution to these problems

  • store a timestamp for every unique id generated
  • change that timestamp to the current timestamp every time the user performs an action* on the page that uses that unique id
  • if the user performs an action* in a page with another unique id, check if x time has elapsed since last action for every unique id, if yes, delete it

*: actions that can be performed on the page and page refresh

Comments

0

You can do this using session_name($unique_script_instance_name) - https://www.php.net/manual/en/function.session-name.php

All you need to do is to define a unique name for each instance of your script, so that for example if you have 2 separate installations of your application on a shared server, you provide name A for one and B for second, you can store this value as $unique_script_instance_name.

Then before you call session_start() you need to call session_name()

// One script
$unique_script_instance_name "A";
session_name($unique_script_instance_name);
session_start();

...

// Second script
$unique_script_instance_name "B";
session_name($unique_script_instance_name);
session_start();

...

That way both scripts will have its own session "domain" and will be isolated from each other. That way

Comments

0

Remember that a session is just a variable. You can definitely create a unique session variable for each page. For each get request a new unique session variable will be created. All you need to do is to create sessions' names unique.

<?php
 session_start();
 $_SESSION[$_GET['pid'].'_p1'] = "a session value";
 $_SESSION[$_GET['pid'].'_p2'] = "a session value";
 $_SESSION[$_GET['pid'].'_p3'] = "a session value";
?>

So for example, if you visit http://example.com/?pid=1232

The session name will be something like $_SESSION['1232_p1'];

For each get request a new unique session will be created.

To see all your session variables, use this code at the end of your page:

<?php
echo '<pre>';
print_r($_SESSION);
echo '</pre>';
?>

Comments

0

This is a very old question, but I am writing it for the use of other programmer or novice friends.

Just set a validation page for every request from client to check session variables. for example: valid_ref.php Or use a reference page for all page in your source code and call other pages according to client requests in the reference page.for example: index.php or home.php or ref.php in valid_ref.php check if $_SESSION[SCRIPT_NAME]['filter_data'] was in session , when client open new tab your web app use Previous data in $_SESSION[SCRIPT_NAME]['filter_data']'s and with this way you dont have any problem with new tabs in browsers from one client.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.