how to create site-independent session variables ?

Each time I gave a new version of my project to our tester then he placed the new project within a new root folder ( a new site then ). Inside the php files there are session variables which are created and used. So the problem comes when he opens two sites at the same time to compare the two releases : with the first site he opens a page , then a session variable is created. Now he opens the second site and go to a particular page then he is directly redirected to a particular page because of the session variable from the previous site. So how to create site-independent session variables ?



2.6k

so how to reference a session variable with this setting ?



64.1k

you dont need ot change your code. Refencing to your variables is all the same. ( i think :-) )



2.6k

I wrote this in the public index.php file :

$di->set('session', function () {
    $t = time();
    $session = new Session(
        array('uniqueId' => $t)
    );
    $session->start();
    return $session;
});

Now after loging in then there is a test in a controller :

public function indexAction()
{

    //menu et navigation
    if ( ! $this->session->has('user_code') ) {
        $this->response->redirect('index');
    } else {
        ...
    }
}

The problem is that now the test always goes to the $this->response->redirect('index'); part although it went to the else part before !

So what is wrong ?



64.1k
edited Feb '16

do it like this :

$di->set('session', function () {
    $session = new Session(
        array('uniqueId' => 'myapp-1')
    );
    $session->start();
    return $session;
});

then you print_r($_SESSION); you should see entries for this app only.

you can check this one also : http://us3.php.net/manual/en/function.session-name.php

and this: http://stackoverflow.com/questions/5479626/same-domain-different-folder-php-session

i think you might be miss matching sessions and cookie paths



2.6k

which version of Phalcon does the isolation session setting work ?



64.1k

https://github.com/phalcon/cphalcon/blob/2.0.0/phalcon/session/adapter.zep

session adapter doesnt exists in 1.x branches so my guess would be after phalcon 2



2.6k

The version I am using is 2.0.8 , and its build date is Sep 26 2015.



64.1k

well based on the source code of the adapter setting it up like i mentioned above, and using $this->session->get('my-stored-value'); should be working perfectly.



2.6k
edited Feb '16

well .... I didnt find how to work with the session isolation setting , so I created customized functions to create unique session variables according to the site name :

function sessionExists($name) { $di = \Phalcon\DI::getDefault(); $session = $di->getSession(); $tabscriptname = explode("/", $SERVER['SCRIPTNAME']); return $session->has($tabscriptname[1].'_'.$name); }

function setSession($name, $value) { $di = \Phalcon\DI::getDefault(); $session = $di->getSession(); $tabscriptname = explode("/", $SERVER['SCRIPTNAME']); $session->set($tabscriptname[1].'_'.$name, $value); }

function getSession($name) { $di = \Phalcon\DI::getDefault(); $session = $di->getSession(); $tabscriptname = explode("/", $SERVER['SCRIPTNAME']); return $session->get($tabscriptname[1].'_'.$name); }