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

Setting the session_name

Hi all,

How can rename the session_name in my controller?

This is my example code which I have in my bootstrap file

/**
* Start the session the first time some component requests the session service
*/
$di->setShared('session', function () {
    try {
        $session = new SessionAdapter();    
        $session->name('oldName');
        $session->start();
        return $session;                
    } catch (Exception $ex) {
        echo "Fatal session error, session not instantiated";
        die;
    }
});

And this is my controller source code

class SessionController extends Phalcon\Mvc\Controllers
{
    public function initialize()
    {
        // destroy the whole session
        $this->session->destroy();
        // set session ID = token
        $token = 123;
        $this->session->setId($token);
        $expire=8*20*10;
        $this->session->start();                    
        // PHP way  session_name("myApp");
        //Phalcon way ???? [session_name, cache_expire, cookie_params etc]          
    }
}

Thanks!

At this point in time I don't think the session adapter by default can support it (based on https://github.com/phalcon/cphalcon/blob/master/ext/session/adapter.c#L498)

You would have to extend Phalcon\Session\Adapter or create your own implementation of it.



22.8k

Thanks Mitchell. Will it be fine then to just use the php version of session_name On 18 Mar 2015 1:43 AM, "Mitchell Macpherson" [email protected] wrote:

Try using this inside your controller

    $this->getDi()->setShared(...)

And name you session with a new name inside the function.