Yes, I am facing the same problem.
I tried this code, but it seems not working.
$client = new Zend_Http_Client("http://www.abc.com");
$client->setCookie('var','value',time() + 2678400,'/');
Could anyone can give us some hints.
Hi,
I want to create a cookie, but it doesn't seem to work on localhost.
Does any body used it before and give me some guidelines other than the documentation?
Snef
Yes, I am facing the same problem.
I tried this code, but it seems not working.
$client = new Zend_Http_Client("http://www.abc.com");
$client->setCookie('var','value',time() + 2678400,'/');
Could anyone can give us some hints.
Check out the setcookie documentation on php.net ... there are some caveats with setting cookies that you may be running into. Also, try setting all of the paramaters, even if you want the defaults (just set the defaults), as this can sometimes cause issues in some browsers, which result in the cookie not being set (without error).
This seems to be a bit of a gotcha with ZF in my opinion.
If you checkout the PHP docs on the normal setcookie method here:
PHP: setcookie - Manual and do a text search on 'localhost', you will find comments saying the following:
"domain names must contain at least two dots (.), hence 'localhost' is invalid and the browser will refuse to set the cookie! instead for localhost you should use false."
This is true, and your app will work if you use setcookie over Zend_Http_Cookie.
[PHP]
setcookie('lang', $_GET['lang'], time() + 7200, '/', false);
[/PHP]
However, using Zend_Http_Cookie and setting the domain as false returns an error:
[PHP]
$cookie = new Zend_Http_Cookie('lang', $_GET['lang'], false, time() + 7200);
[/PHP]
[HTML]
Fatal error: Uncaught exception 'Zend_Http_Exception' with message 'Cookies must have a domain' in C:\wamp\www\ewhq\library\Zend\Http\Cookie.php
[/HTML]
Not a huge biggy - but it would be nice for the people developing ZF locally to be able to use the ZF cookie library and all the methods that go with it.
Aaron
Aaron-
Why don't you just set-up a 'local domain' via the hosts file? Since this file is read before DNS, you can make it any domain you want. If you do gothat route, then Zend_Http_* will work fine.
Of course, you should also set your PHP 'cookie_domain' via the php.ini or .htaccess, etc.
I was working with these locally last week on a sub-project using ZF v174, so I know it can be done. -
Jay
Cheers Jay,
I got around it by just using straight PHP setcookie().
Yeah I use my host file, but my domains are always like [project].localhost. I could make them fully qualified I guess. I was just curious that ZF's cookie library didn't adhere to PHP standards.
Must be a reason for it.
Aaron