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 Cookie adapter

Hello,

I'm trying to build a session driver with cookie. It seems that "write" method works ok and creates cookie with value provided. But "read" method cannot retrieved the cookie value.

Can someone help me?

Regards, Velizar

<?php

class Cookie extends Adapter implements AdapterInterface
{
    protected $isDestroyed = false;

    public function __construct($options = null)
    {
        parent::__construct($options);

        session_set_save_handler(
            array($this, 'open'),
            array($this, 'close'),
            array($this, 'read'),
            array($this, 'write'),
            array($this, 'destroy'),
            array($this, 'gc')
        );
    }

    public function open()
    {
        return true;
    }

    public function close()
    {
        return false;
    }

    public function read($sessionId)
    {
       return $_COOKIE[$sessionId]; // everytime this line returns an error undefined index
    }

    public function write($sessionId, $data)
    {
        secookie($sessionId, $data, .....);
    }

    public function destroy($session_id = null)
    {

    }

    public function gc($maxlifetime)
    {

    }
}


2.1k
Accepted
answer

First of all that is REALLY not recommended.

but here goes.

https://php.net/manual/en/function.setcookie.php

please note that if cookie is turned off for the browser this WILL NOT WORK.

and your write?

secookie($sessionId, $data, .....);// << setcookie you missed the t



1.0k

Hello,

Thank you for your reply. This was a typo in my comment. I agree that this approach is not recommended.

Regard

First of all that is REALLY not recommended.

but here goes.

https://php.net/manual/en/function.setcookie.php

please note that if cookie is turned off for the browser this WILL NOT WORK.

and your write?

secookie($sessionId, $data, .....);// << setcookie you missed the t

Did you ever get anywhere with this?