Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

set cookie in zend framework

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?

like image 761
Rukmi Patel Avatar asked Mar 01 '12 10:03

Rukmi Patel


4 Answers

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

like image 87
Tim Fountain Avatar answered Oct 24 '22 10:10

Tim Fountain


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

like image 26
shukshin.ivan Avatar answered Oct 24 '22 09:10

shukshin.ivan


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
like image 1
Rikesh Avatar answered Oct 24 '22 08:10

Rikesh


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
like image 1
Sam Arul Raj T Avatar answered Oct 24 '22 09:10

Sam Arul Raj T