Yii Framework Forum: handling Cookies - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

handling Cookies Rate Topic: -----

#1 User is offline   DarkNSF 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 287
  • Joined: 12-November 08
  • Location:Palm Bay, Florida

Posted 30 January 2011 - 11:34 PM

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]);
  }

0

#2 User is offline   zaccaria 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 2,232
  • Joined: 04-October 09
  • Location:Moscow

Posted 31 January 2011 - 02:13 AM

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.
0

#3 User is offline   DarkNSF 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 287
  • Joined: 12-November 08
  • Location:Palm Bay, Florida

Posted 15 February 2011 - 01:44 PM

View Postzaccaria, on 31 January 2011 - 02:13 AM, said:

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.
0

#4 User is offline   Trejder 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,197
  • Joined: 06-October 10
  • Location:Southern Poland

Posted 26 February 2011 - 06:38 AM

View PostDarkNSF, on 30 January 2011 - 11:34 PM, said:

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.

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.
Proud Cookbook author, though still learning powerful Yii! :] See my generic profile for more information. Cheers!
0

#5 User is offline   inteist 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 3
  • Joined: 08-March 12

Posted 13 April 2012 - 11:59 AM

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]);
    }


0

#6 User is offline   Al @ Perch Ten 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 1
  • Joined: 13-May 13

Posted 03 January 2014 - 03:40 PM

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]);
    }

}

0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users