Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

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)
    {

    }
}


1.4k
Accepted
answer

First of all that is REALLY not recommended.

but here goes.

http://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

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.

http://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?