Search code examples
phpzend-frameworkcookiessession-cookiessetcookie

how to set cookie in zend framework?


I am trying to use Zend Framework to set a cookie but I don't understand how it works.

$cookie = md5("_somerandomstring_");

Zend_Loader::loadClass("Zend_Http_Client");

require_once "Zend/Http/Cookie.php";

$client = new Zend_Http_Client();

$cookie = new Zend_Http_Cookie('foo', $cookie, '.mydomain.com', time()+7200, '/path');

$client->setCookieJar();
$client->setCookie($cookie);

How can I set the .mydomain.com when I am run my site on localhost?


Solution

  • You could add the domain as an environment-specific config entry in application/configs/application.ini.

    [production]
    cookieDomain = "example.com"
    
    [development:production]
    cookieDomain = "localhost"
    

    Then use that domain as the third arg in Zend_Http_Cookie constructor.

    This way, when running in development mode, you can set the domain to be "localhost", while running in production, you can set it to be, say, "example.com".

    Note: I'd still use a virtual-host - named something like "example.local" - for running locally in dev mode. For example, if you were developing two different projects using "localhost", cookies that you set for those two projects on that single domain could suffer from name-collision.