Cookie Management


Zend Framework – Cookie Management


”;


The Cookie is a very important concept in a web application. It provides the option to persist the user”s data, usually a small piece of information in the browser itself for a limited period.

A Cookie is used to maintain the state of the web application. Zend framework provides a cookie module inside the zend-http component. This zend-http provides the HTTP abstraction and its implementation.

Installing the HTTP Component

The HTTP component can be easily installed using the Composer as specified in the code below.

composer require zendframework/zend-http 

Concept

The zend-http provides the ZendHttpCookies class to manage cookies. It is used along with the ZendHttpClient class, which is used to send a request to a web server. Cookies can be initialized as shown in the code below −

use ZendHttpCookies  
$c = new Cookies(); 

When the HTTP client (ZendHttpClient) first sends a URI request to the web server, it does not have any cookie. Once the request is received by the web server, it includes the cookie in its response object as the HTTP Header, Set-Cookie and sends it to the HTTP client. The HTTP client will extract the cookie from the http response and resent it as same HTTP Header in the subsequent request. Generally, each cookie will be mapped to a domain and a path of the domain.

The methods available in Cookies class are as follows −

  • addCookie(uri) − It is used to add a cookie into the request object of the given URI.

  • getCookie(cookieName, $cookieForm) − It is used to get the cookie, $cookieName available in the given URI, $uri. The third argument is how the cookie will be returned, either string or array.

  • fromResponse(uri) − It is used to extract cookies from the response object of the given URI.

  • addCookiesFromResponse − It is same as fromResponse, but it extracts and adds it again into the request object of the given URI.

  • isEmpty() − It is used to find whether the given Cookie object has any cookie or not.

  • reset() − It is used to clear all the cookies in the given URI.

In the next chapter, we will discuss regarding session management in the Zend Framework.

Advertisements

”;

Leave a Reply

Your email address will not be published. Required fields are marked *