Set cookie in zend framework

I am new to zend framework. I wrote this code to set a cookie on my site.

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 don’t even know, having written this code, is my cookie set or not? Now if I want to get a cookie, how can I do this?

+6
source share
5 answers

Zend_Http_Cookie not intended to set cookies. This is the class used by Zend_Http_Client to send and receive data from sites that require cookies. To set cookies, simply 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 them on subsequent requests using $_COOKIE['user_email_id'] and $_COOKIE['user_password'] ; or if you use ZF MVC classes: $this->getRequest()->getCookie('user_email_id') (from the controller method).

+16
source

Check Zend_Http_Cookie

You will receive your cookie as follows:

 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 
+1
source

Use this method to do this.

in your controller do this 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 
+1
source

Your cookies are set by sending a response. You can change the answer 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 a response when it has finished sending the request; usually you will never need to call him. http://framework.zend.com/manual/1.12/en/zend.controller.response.html

+1
source

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

Link: Zend Cookies

0
source

Source: https://habr.com/ru/post/909728/


All Articles