February 29, 2012

Difference between normal cookies and Zend_http_cookies in PHP

Question by Kunal

I am learning a Zend Framework. Its going really difficult to me as compare to CodeIgniter. I have a problem of the difference between normal cookies(php) and zend_http_cookies. I am using normal cookies in my Zend application, it works but I want to understand Zend_http_cookies and its pure concept, can anyone tell me this..
thnx in advance.

Answer by Kunal

PHP cookie :

A cookie is often used to identify a user. A cookie is a small file that the server embeds on the user’s computer. Each time the same computer requests a page with a browser, it will send the cookie too. With PHP, you can both create and retrieve cookie values.

In PHP you can set cookie like this :

Example :

setcookie("user", "username", time()+3600);
//For getting cookie write:
echo $_COOKIE["user"];

Zend_Http_Cookie:

Zend_Http_Cookie is a class that represents an HTTP cookie. It provides methods for parsing HTTP response strings, collecting cookies, and easily accessing their properties.

You can instantiate it using :

$cookie = new Zend_Http_Cookie('user',
                               'user demo',
                               '.example.com',
                               time() + 3600,
                               '/path');

A cookie object can be transferred back into a string, using the __toString() magic method. This method will produce a HTTP request “Cookie” header string, showing the cookie’s name and value, and terminated by a semicolon (‘;’). The value will be URL encoded, as expected in a Cookie header:

Example:

// Will print out 'user=user+demo;' :

echo $cookie->__toString();

echo (string) $cookie;

echo $cookie;

For advance you should read zend documentation from framework.zend.com

Answer by Starx

Zend_Http_Cookie is not related to normal cookies. It is a part of Zend_Http_Client and is a class that represents an HTTP cookie.

It provides methods for parsing HTTP response strings, collecting cookies, and easily accessing their properties. It also allows checking if a cookie matches against a specific scenario, IE a request URL, expiration time, secure connection, etc.

Reference

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!