I am new to zend framework. I have write this code to set cookie in my website.
public function setCookie($data){
$email_cookie = new Zend_Http_Cookie('user_email_id', $data['user_email_id'], $_SERVER['HTTP_HOST'], '', FALSE);
$pass_cookie = new Zend_Http_Cookie('user_password', $data['user_password'], $_SERVER['HTTP_HOST'], '', FALSE);
$cookie_jar = new Zend_Http_CookieJar();
$cookie_jar->addCookie($email_cookie);
$cookie_jar->addCookie($pass_cookie);
}
I dont even know by writing this code, my cookie is set or not?
now If I want to retrieve the cookie then how can I do it?
Zend_Http_Cookie
is not for setting cookies. It is a class used by Zend_Http_Client
for sending and receiving data from sites that require cookies. To set cookies just use the standard PHP setcookie() function:
setcookie('user_email_id', $data['user_email_id'], time() + 3600, '/');
setcookie('user_password', $data['user_password'], time() + 3600, '/');
this will set cookies that expire in 1 hour. You can then access these on subsequent requests using $_COOKIE['user_email_id']
and $_COOKIE['user_password']
; or if you are using ZF's MVC classes: $this->getRequest()->getCookie('user_email_id')
(from a controller method).
Your cookies are set by sending response. You can modify response in your code.
$cookie = new Zend_Http_Header_SetCookie();
$cookie->setName('foo')
->setValue('bar')
->setDomain('example.com')
->setPath('/')
->setHttponly(true);
$this->getResponse()->setRawHeader($cookie);
By default, the front controller sends response when it has finished dispatching the request; typically you will never need to call it.
http://framework.zend.com/manual/1.12/en/zend.controller.response.html
Check Zend_Http_Cookie
You will get your cookie like following:
echo $email_cookie->getName(); // user_email_id
echo $email_cookie->getValue(); // Your cookie value
echo ($email_cookie->isExpired() ? 'Yes' : 'No'); // Check coookie is expired or not
Use this way you can do it
in your controller do it code as
$cookie = new Zend_Http_Cookie('cookiename',
'cookievalue',
time() + 7200 //expires after 2 hrs
);
echo $cookie->__toString();
echo $cookie->getName(); //cookie name
echo $cookie->getValue(); //cookie value
Try:
$ret_as = COOKIE_STRING_ARRAY;
Zend_Http_CookieJar->getAllCookies($ret_as);
//Get all cookies from the jar. $ret_as specifies the return type
//as described above. If not specified, $ret_type defaults to COOKIE_OBJECT.
Ref: Zend Cookies