We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

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 ?



7.2k

so how to reference a session variable with this setting ?



85.5k

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



7.2k

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 ?



85.5k
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 : https://us3.php.net/manual/en/function.session-name.php

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

i think you might be miss matching sessions and cookie paths



7.2k

which version of Phalcon does the isolation session setting work ?



85.5k

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



7.2k

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



85.5k

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.



7.2k
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(); $tab_script_name = explode("/", $_SERVER['SCRIPT_NAME']); return $session->has($tab_scriptname[1].''.$name); }

function setSession($name, $value) { $di = \Phalcon\DI::getDefault(); $session = $di->getSession(); $tab_script_name = explode("/", $_SERVER['SCRIPT_NAME']); $session->set($tab_scriptname[1].''.$name, $value); }

function getSession($name) { $di = \Phalcon\DI::getDefault(); $session = $di->getSession(); $tab_script_name = explode("/", $_SERVER['SCRIPT_NAME']); return $session->get($tab_scriptname[1].''.$name); }