cphalcon icon indicating copy to clipboard operation
cphalcon copied to clipboard

[NFR] Implement \Phalcon\Http\Cookie()

Open trsrm opened this issue 14 years ago • 12 comments

It would be great if you implement the class that represents the HTTP cookie. It will provide easy access to $_COOKIE params and easy way to set cookies.

It can has the same workflow as in Zend Framework:

// we can set cookie using constructor:
$cookie = new \Phalcon\Http\Cookie(
    'foo',
    'bar',
    time() + 3600,
    '/path'
    'domain.com',
);

// or setter:
$cookie = new \Phalcon\Http\Cookie()
$cookie->set('foo', 'bar', time() + 3600, '/path', 'domain.com');

// than we can get cookie using constructor:
$cookie = new \Phalcon\Http\Cookie('foo');

// or getter:
$cookie = new \Phalcon\Http\Cookie();
$cookie->get('foo');

echo $cookie->getName(); // will echo 'foo'
echo $cookie->getValue(); // will echo 'bar'
echo $cookie->getDomain(); // will echo 'domain.com'
echo $cookie->getPath(); // will echo '/path'
echo $cookie->getExpiryTime(); // will echo expiration timestamp
echo $cookie->isExpired() ? 'y' : 'n'; // will return 'y' or 'n'

Ideally it can be accessible in the action:

class IndexController extends \Phalcon\Mvc\Controller
{
    public function getAction()
    {
        $cookie = $this->cookie->get('foo');
    }

    public function setAction()
    {
        $this->cookie->set(
            'foo',  'bar', time() + 3600, '/path', 'domain.com'
        );
    }
}

What do you think about it?

Regards, Taras Romaniv

trsrm avatar Oct 19 '12 18:10 trsrm

I think it can be handy if it provides extra functionality, else it's exact same (except giving OO interface) as setcookie() function and _$COOKIE super-global.

There can be a flag to make cookie encrypted. This helper can encrypt and decrypt cookie transparently. So it becomes more handy.

hdogan avatar Oct 20 '12 14:10 hdogan

hai, this can be under the Phalcon\Http\Request namespace since it's part of the HTTP request:


//return a \Phalcon\Http\Request\Cookie
$cookie = $this->request->getCookie('foo'); 

$cookie->setValue('bar');

kenjikobe avatar Oct 21 '12 04:10 kenjikobe

Yes, but setting the cookie is in the response. I think it'd be better to leave it in Phalcon\Http. Something like this would be ok: https://github.com/Xeoncross/micromvc/blob/master/Class/Core/Cookie.php

sraka1 avatar Nov 01 '12 18:11 sraka1

How this part works after refresh the page, it returns empty values?

echo $cookie->getDomain(); // will echo 'domain.com'
echo $cookie->getPath(); // will echo '/path'
echo $cookie->getExpiryTime(); // will echo expiration timestamp
echo $cookie->isExpired() ? 'y' : 'n'; // will return 'y' or 'n'

ghost avatar Nov 24 '12 18:11 ghost

I'm going to move this feature to 0.8.0 waiting for the required feedback

ghost avatar Nov 25 '12 06:11 ghost

Closed since nobody answered

ghost avatar Dec 21 '12 01:12 ghost

Hi,

The idea wasn't bad, I re-open this thread considering it's the exact same subject ...

I tried a ...

$cookie = new \Phalcon\Http\Cookie();
$cookie->set('foo', 'bar', time() + 3600, '/path', 'domain.com');

... without success because my cookie has to be set with my class, and each class is linked with a cookie, which isn't very handy for me (if I want to set many cookies in a row, or if i want to "setShared" these classes).

Will you work on it ? Or maybe it's me that did something wrong, i'm quiet new with Phalcon !

Linkbreakers avatar Feb 02 '13 20:02 Linkbreakers

Why do you need this feature? It brings nothing new. Why you don't just use setcookie method? Or, if you need to have collection for all cookies available in the DI, i would do something like this:

class CookieManager
{
    /**
     * @var Cookie[]
     */
    protected $cookies = [];

    /**
     * Constructor
     * @return void
     */
    public function __construct()
    {
        foreach($_COOKIE as $name => $value) {
            $this->cookies = new Cookie([
                'name'  => $name,
                'value' => $value
            ]);
        }
    }

    /**
     * Getter
     * @param string $name
     * @return Cookie
     */
    public function __get($name)
    {
        if (isset($this->cookies[$name])) {
            return $this->cookies[$name];
        }
    }

    /**
     * Setter
     * @param string $name
     * @param Cookie $cookie
     */
    public function __set($name, $cookie)
    {
        $this->cookies = $this->cookies[$name] = $cookie;
    }

    /**
     * Sets a cookie
     * return bool
     */
    public function set(/* options from setcookie method */)
    {
        $cookie = new Cookie([
            'name' => $name, 
            'value' => $value
        ]);

        // sets a cookie
        if (setcookie(/* all cookie options here */)) {
            $this->cookies[$name] = $cookie;
            return true;
        } else {
            return false;
        }

    }
}

class Cookie
{
    /**
     * Cookie name
     * @var string
     */
    protected $name;

    /**
     * Cookie value 
     * @var mixed
     */
    protected $value;

    /**
     * Constructor
     * @param array $params
     * @return void
     */
    public function __construct(array $params)
    {
        foreach($params as $name => $value) {
            if (property_exists($this, $name)) {
                $this->$name = $value;
            }
        }
    }

    /**
     * Getter
     * @param string $name
     * @return mixed
     */
    public function __get($name)
    {
        if (property_exists($this, $name)) {
            return $this->$name;
        }
    }

    /**
     * Setter
     * @param string $name
     * @param mixed $value
     * @return mixed
     */
    public function __set($name, $value)
    {
        if (property_exists($this, $name)) {
            return $this->$name = $value;
        }
    }
}

So, after all you can set CookieManager as shared service in DI and call it when you need it.

alvassin avatar Feb 02 '13 20:02 alvassin

I was asking because it appeared logical to create something that could set more than one cookie but I didn't get the correct use of this framework ; it's true, there's nothing new at the end !

Your class is well-built, perfectly what I wanted, thank you !

Linkbreakers avatar Feb 02 '13 21:02 Linkbreakers

PHP message: PHP Parse error: syntax error, unexpected '[', expecting ')' in CookieManager.php on line 16 :(

jg77merten avatar Dec 07 '15 16:12 jg77merten

@jg77merten PHP version have to be at least 5.4

valVk avatar Dec 13 '15 20:12 valVk

@Linkbreakers PHP 5.3 has many security issues and no EOL since more than a year. Stop using it

sergeyklay avatar Dec 13 '15 20:12 sergeyklay