English 中文(简体)
Cookie Management
  • 时间:2024-11-03

Zend Framework - Cookie Management


Previous Page Next Page  

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

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

Instalpng 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 ZendHttpCpent class, which is used to send a request to a web server. Cookies can be initiapzed as shown in the code below −

use ZendHttpCookies  
$c = new Cookies(); 

When the HTTP cpent (ZendHttpCpent) 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 cpent. The HTTP cpent 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