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

Session Problem

hi guys,

I'm stuck with this problem, my session is still present even if i already remove it, session still showing when I click the back button, unlike INVO when you click logout and press back button it shows blank page with warning/error message

here is my code

services.php

<?php

use \Phalcon\Session\Adapter\Files as SessionAdapter;

$di->set('session', function () {
    $session = new SessionAdapter();
    $session->start();

    session_regenerate_id();
    return $session;
});

SessionController.php

<?php
class SessionController extends ControllerBase
{
    public function indexAction()
    {

    }

    /* get user from login/register form */
    public function registerAction(){

        $username = "testsession";

        $this->_session($username);
    }

    /* making session to be accessable */
    private function _session($usersession){

        $this->session->set('auth', $usersession);
    }

    /* end session */
    public function endAction(){

        $this->session->remove('auth');
        $this->forward('home/');
    }   

}

my Elements.php

<?php

use \Phalcon\Mvc\User\Component as Element,
        \Phalcon\Tag as Tag;
/**
 * Elements
 *
 * Helps to build UI elements for the application
 */
class Elements extends Element
{
    private $_tabs = array(
        'Home' => array(
            'controller' => 'home',
            'action' => 'index',
            'any' => false
        ),
        'Test1' => array(
            'controller' => 'test1',
            'action' => 'index',
            'any' => false
        ),
        'Test2' => array(
            'controller' => 'test2',
            'action' => 'index',
            'any' => false
        )
    );

    /**
     * Builds header menu with left and right items
     *
     * @return string
     */

    public function getTabs()
    {
        /* get session value in HomeController startSession method */
        $auth = $this->session->get('auth');

        echo $auth;

        if(!empty($auth)){

            $controllerName = $this->view->getControllerName();
            $actionName = $this->view->getActionName();
            echo'<ul class="nav navbar-nav navbar-right">';
                    foreach ($this->_tabs as $caption => $option){
                        if($option['controller'] == $controllerName && ($option['action'] == $actionName || $option['any'])){
                            echo'<li class="active">';
                        }
                        else{
                            echo'<li>';
                        }
                        echo Tag::linkTo($option['controller'].'/'.$option['action'], $caption), '</li>';

                    }

            echo'</ul>';

            echo Tag::linkTo('session/end', 'Log out');
        }
        else{

            $controllerName = $this->view->getControllerName();
            $actionName = $this->view->getActionName();

            echo'<ul class="nav navbar-nav navbar-right">';
                    foreach ($this->_tabs as $caption => $option){
                        if($option['controller'] == $controllerName && ($option['action'] == $actionName || $option['any'])){
                            echo'<li class="active">';
                        }
                        else{
                            echo'<li>';
                        }
                        echo Tag::linkTo($option['controller'].'/'.$option['action'], $caption), '</li>';
                    }
            echo'</ul>';
        }

    }
}


6.6k

Maybe you have to use a shared instance of the session? I think everytime you access $di->session a new session instance is created...

$di->setShared('session', function() {
    $session = new Phalcon\Session\Adapter\Files();
    $session->start();
    return $session;
});


25.7k
edited Jan '15

Although I'm still on my way looking for my answer, there is something hopefully helpful to you: shared-services see that

$di->get('session');

$di->getSession();

however I can't tell anything deeper at this moment. What I'm also looking for is to manage session data at all different location, for example anywhere outside of injectable. I'm confused that what's the "correct" way to manage session data, for example if it is not a "shared service", which function should I use? If it is a "shared service", then which one is "correct"? What's the pros and cons of them?