Cookies in PHP
For those new to web development and ‘how things work’, cookies can be a very confusing matter. In this tutorial, Timothy gives you an overview of cookies to help you understand how they work.
A cookie is a small bit of information stored on a viewer’s computer by his or her web browser by request from a web page. The information is constantly passed in HTTP headers between the browser and web server; the browser sends the current cookie as part of its request to the server and the server sends updates to the data back to the user as part of its response.
The size of a cookie depends on the browser but in general should not exceed 1K (1,024 bytes). The information can really be anything… it can be a name, the number of visits to the site, web based shopping cart information, personal viewing preferences or anything else that can be used to help provide customized content to the user.
In addition to the information it stores, each cookie has a set of attributes: an expiration date, a valid domain, a valid domain path and an optional security flag. These attributes help ensure the browser sends the correct cookie when a request is made to a server.
Let’s look at an example that uses a very basic cookie:
<?php |
The two points of interest are $_COOKIE and setcookie.
Cookie information sent by the browser is accessed with $_COOKIE, a super global array containing cookie values keyed by name.
The setcookie function is used to assign a key/value pair to be sent back to the client. The client uses the new values to update its cookies if the information already exists. If the information doesn’t exist, new cookies will be created.
Remember that cookie information is exchanged within HTTP headers; cookies must be sent before the script generates any output.
Original Author : Click Here
..