Contents
Using PHP and cookie
The sending data from the one web page to another also may be required, for example, in an e-commerce web-site, when it's essential that you store the contents of a shopping cart while the user is browsing your site.
In order to do this, there are two easy ways: you either use cookies or sessions.
Cookies presents a small amounts of data stored by the user's browser after a request from a server or script but they have some limitations like the maximum number of cookies from a host that can be stored by a browser is 20, and the maximum cookie size is 4KB.
Main thing about cookies that should be considered while using them is that only the originating host can read the stored data, thus the user's privacy is respected.
Also, user can choose to be notified by the browser when accepting a cookie and can reject the cookie.
Cookies consist of a name, value, expiry date, host and path information, and they end up to the user because they are send from the server through an HTTP header.
To set a cookie with PHP, you can use the "header()" function, or the "setcookie()" function as shown at the example below.
//don't output anything before this...
header("visits=23; expires=Friday, 01-Dec-07 01:00:00 GMT; path=/;domain=test.com");
setcookie("hits", 23, time() + 3600, "/", "test.com", 0); //notice this last extra argument
Both statements are used to send a cookie to the user's web-browser and the values at the end of the statement expressed as 0 or 1 are for weather the cookies will be send only over a secure connection (0 means no, 1 means yes).
The web-server reads the information only when the browser sends it the cookie, and this will not happen until the user revisits the web-page.
The "$visits" variable will not be created after we send the header.
Expiry date set to zero will make the browser use the cookie until the user closes it, the browser won't remember the cookie the next time it's started.
Set the cookie you want to delete to date that has already expired.
Also include the same path, domain and secure parameters you originally used when setting the cookie: