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 get all created Phalcon session variables ?

In PHP we can loop the $_SESSION global variable to get all PHP session variables. But if I created Phalcon session variables how can I get all of them ?

edited Feb '16

Sorry i missunderstood you in first place. Will check it up and come to you if i find what you wnat.

Checked adapter soruce files, but i cannot find such method. Maybe it is intentional to not implement it?

However when storing data with Phalcon sessions the data is available in $_SESSIOn as well. So if you have a specific case to get all session data you can access it. Or even make your own session class with method like:

function getAll()
{
    return $_SESSION; 
}

I never needed to do something like you asked, but let's hope someone has fought with similar problem and will tell us :)



7.2k

no ! Phalcon sessions data are not available in $_SESSION !

Are you sure? For me printing $_SESSION has same variables as Phalcon's session. What phalcon version are you using? How did you setup your session service?



7.2k
edited Feb '16

my Phalcon version is 2.0.8 , build date Sep 26 2015 16:24:18.

I setup session service in the public index.php file :

[code]<?php

use Phalcon\Loader; use Phalcon\DI\FactoryDefault; use Phalcon\Mvc\View; use Phalcon\Mvc\Application; use Phalcon\Mvc\Url as UrlProvider; use Phalcon\Db\Adapter\Pdo\Mysql as DbAdapter; use Phalcon\Session\Adapter\Files as Session;

try {

// Register an autoloader
$loader = new Loader();
$loader->registerDirs(array(
    '../app/controllers/',
    '../app/models/'
))->register();

// Create a DI
$di = new FactoryDefault();

// Start the session the first time when some component request the session service
$di->setShared('session', function () {
    $session = new Session();
    $session->start();
    return $session;
});

...

//Handle the request
$app = new Application($di);

echo $app->handle()->getContent();

} catch(\Exception $e) { echo "Exception: ", $e->getMessage(); }

?>[/code]



7.2k

I found the solution : I must call this code in order to synchronize Phalcon sessions and PHP session :

$di = \Phalcon\DI::getDefault();
$session = $di->getSession();

So Why should I call this code to synchronize Phalcon sessions and PHP sessions ?