Get Cookie values with Zend Framework

15,027

Solution 1

getCookie() method is not static, it should be called on an object.

I believe this code is from your controller, so it should basically look like

$request = $this->getRequest();
$cookieData = $request->getCookie('someCookie', 'default');

Solution 2

This is a slight side note, yet it may just well help avoid long fruitless hours. From my experience, the problems that occur when one cannot retrieve value from $_COOKIE in zf1 and other frameworks occur mostly because setCookie is so easy to use one forgets to add the path and the domain like so:

setcookie('cookieName', 'cookieValue', $finalExpirationTime,'/','.yourdomain.com');

and instead do this:

setcookie('cookieName', 'cookieValue', $finalExpirationTime);

This gets real annoying especially so when working on Windows with ip's instead of actual domains. Another thing to look out for would be the dot (.) in front of the domain. As stated in the manual: Older browsers still implementing the deprecated » RFC 2109 may require a leading . to match all subdomains.

Hope this helps

Share:
15,027
fer sid
Author by

fer sid

If this Infrastructure works, it was build by me - if not .. eh someone else! When the sun shines: Working as one man DevOps Crew in Hamburg (StartUp). In love with Docker, Git, PHP (got a certification decades ago). I prefer Cloud, don't like Baremetal stuff. Deep into AWS and Azure.

Updated on June 04, 2022

Comments

  • fer sid
    fer sid almost 2 years

    Warning: Non-static method Zend_Controller_Request_Http::getCookie() should not be called statically in..

    Iam trying the following to get Cookie values:

    $cookieData = Zend_Controller_Request_Http::getCookie($key, $default);
    

    is there an better way to this?

  • fer sid
    fer sid over 13 years
    No it's part of an library (-: And there is no Request object
  • Vika
    Vika over 13 years
    Not sure what you mean by library. Is this code called within a Model?
  • almaruf
    almaruf over 9 years