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

Load custom session_id and edit

Hello! I try to figure out how to start session with custom session id.

Basicaly from admin panel I want to send a request to API with a session id hash, API need to load it (something like session_id($sessionid); session_start() and if exists return informations from that session.

Is this possible ? Thanks!

Session ID is being sent to the client's browser in a Set-Cookie HTTP directive, now what exactly do you want to do with request to an external API which is not running a browser?



6.0k

What I asked has nothing to do with cookies.

This is what I want to do with phalcon API, but didn't find a way to do it with session service:


session_id($_GET['session_id']);
session_start();
die(json_encode($_SESSION));


79.0k
Accepted
answer

What I said has nothing to do with the Cookies itself, it's about how generated sessionID (what you asked for) acts as a glue between client's browser and your server. Now what you provided here with session in plain PHP does not make any sense to me, but here it is - Phalcon's version of that code logic of yours:

$di->setShared('session', function () {
    $request = $this->getRequest(); //we need Request service

    $session = new \Phalcon\Session\Adapter\Files(); //or any other supported adapter
    $session->setName('MySessionCookieName');
    $session->setId($request->getQuery('session_id', 'alphanum', $session->getId())); //filter and default value are optional, as I have no clue what you want to achieve with this
    $session->start();
    return $session;
});

$notSureWhy = json_encode($di->getShared('session')->get('youNeedToKnowKeyWhichHoldsYourSessionDataHere'));


6.0k

Perfect, thank you!