handling Cookies

Thought this might be useful to someone… I modified my WebUser class to contain some methods for handling cookie manipulation similar to how flash messages are handled.


  public function hasCookie($name)

  {

    return !empty(Yii::app()->request->cookies[$name]->value);

  }


  public function getCookie($name)

  {

    return Yii::app()->request->cookies[$name]->value;

  }


  public function setCookie($name, $value)

  {

    $cookie = new CHttpCookie($name,$value);

    Yii::app()->request->cookies[$name] = $cookie;

  }


  public function removeCookie($name)

  {

    unset(Yii::app()->request->cookies[$name]);

  }

Nice job, it looks like some shortcut for often used function.

If you are interested, Qiang propose a method for reduce a lot more typing.

I saw this post, not really a big fan, it looks as if there is a local function called user() in the particular file you are editing.

Just wanted to let you (and others) know, that I wrote a Wiki article about cookie management, where I used a small piece of your code.

A minor improvement to make the setting of the cookie more general:




    public function hasCookie($name)

    {

        return !empty(Yii::app()->request->cookies[$name]->value);

    }


    public function getCookie($name)

    {

        return Yii::app()->request->cookies[$name]->value;

    }


    public function setCookie($name, $value, $time = 0, $disableClientCookies = false)

    {

        $cookie = new CHttpCookie($name, $value);

        $cookie->expire = time() + $time;

        $cookie->httpOnly = $disableClientCookies;   

        Yii::app()->request->cookies[$name] = $cookie;

    }





    public function removeCookie($name)

    {

        unset(Yii::app()->request->cookies[$name]);

    }




Another bit of a development, including some human-friendly time handling from 3ft9.com. (I can’t link as it’s my first post, but do a search for “snippet-cookie-class-for-php”)

I’ve also added in a couple of lines to allow path and domain management using Yii app params. Obviously change to what suits or remove if you want.


class Cookie {


    const Session = null;

    const OneDay = 86400;

    const SevenDays = 604800;

    const ThirtyDays = 2592000;

    const SixMonths = 15811200;

    const OneYear = 31536000;

    const Lifetime = -1; // 2030-01-01 00:00:00




    public static function hasCookie($name)

    {

        return !empty(Yii::app()->request->cookies[$name]->value);

    }


    public static function getCookie($name)

    {

        return Yii::app()->request->cookies[$name]->value;

    }


    public static function setCookie($name, $value, $time = null, $disableClientCookies = false)

    {

        if ($time === -1)

            $time = 1893456000; // Lifetime = 2030-01-01 00:00:00

        elseif (is_numeric($time))

            $time += time();

        else

            $time = strtotime($time);


        $cookie = new CHttpCookie($name, $value);

        $cookie->expire = $time;

        $cookie->httpOnly = $disableClientCookies;

        $cookie->domain = Yii::app()->params['cookieDomain'];

        $cookie->path = Yii::app()->params['cookiePath'];

        Yii::app()->request->cookies[$name] = $cookie;

    }


    public static function removeCookie($name)

    {

        unset(Yii::app()->request->cookies[$name]);

    }


}