PDA

View Full Version : PHP Cookies Tutorial


alex123
11-26-2008, 07:14 AM
A cookie is a flat file that stores tiny bits of information. It is stored on the client’s machine and is passed to the client when they visit your site. Each cookie can store anything from usernames to number of visits to a site. This tutorial will teach you how to create, retrieve, display, and delete cookies using php
1. Create a Cookie using PHP

Cookies are created using the setcookie() function. Below is an example of the setcookie() function
setcookie($name, $value, $expired, $path, $domain, $secure)
There are values that you can pass into the setcookie() function. The values are: name of the cookie, value of the cookie, the date the cookie expires, path where the cookie is available on the server, domain where the cookie is available, and where it is coming from a secure page.
The three values that we are worried about are $name, $value, and $expired.
Below is the php code to create a php cookie named "username". We set the value of the cookie to "Tom", and set the expiration date at one hour from now.
<?php
setcookie("username", "Tom", time()+3600);
//http://www.infysolutions.com
?>
The cookie is set through the browser once a client visits your site or page that you set the cookie on.

2. Retrieve & Display a Cookie using PHP

Now let’s retrieve & display the cookie we just set. We retrieve the cookie using the predefined $_COOKIE array. Then we display the cookie using the echo function. Here is the code that will get you the cookie.
<?php
echo $_COOKIE["username"];
?>

Paz
12-08-2008, 08:26 PM
Hi Alex, thanks for sharing some of the mysteries of cookies to us.

Jenniferlinn
12-29-2008, 12:10 PM
Here are some more details about the topic:

A cookie is information that a Web site puts on your hard disk so that it can remember something about you at a later time. (More technically, it is information for future use that is stored by the server on the client side of a client/server communication.) Typically, a cookie records your preferences when using a particular site. Using the Web's Hypertext Transfer Protocol (HTTP), each request for a Web page is independent of all other requests. For this reason, the Web page server has no memory of what pages it has sent to a user previously or anything about your previous visits. A cookie is a mechanism that allows the server to store its own information about a user on the user's own computer. You can view the cookies that have been stored on your hard disk (although the content stored in each cookie may not make much sense to you). The location of the cookies depends on the browser. Internet Explorer stores each cookie as a separate file under a Windows subdirectory. Netscape stores all cookies in a single cookies.txt fle. Opera stores them in a single cookies.dat file.

HTTP cookies are used by Web servers to differentiate users and to maintain data related to the user during navigation, possibly across multiple visits. HTTP cookies were introduced to provide a way to implement a "shopping cart" (or "shopping basket"),[1][2] a virtual device into which the user can "place" items to purchase, so that users can navigate a site where items are shown, adding or removing items from the shopping basket at any time.

Allowing users to log in to a website is another use of cookies. Users typically log in by inserting their credentials into a login page; cookies allow the server to know that the user is already authenticated, and therefore is allowed to access services or perform operations that are restricted to logged-in users.

Many websites also use cookies for personalization based on users' preferences. Sites that require authentication often use this feature, although it is also present on sites not requiring authentication. Personalization includes presentation and functionality. For example, the Wikipedia website allows authenticated users to choose the webpage skin they like best; the Google search engine allows users (even non-registered ones) to decide how many search results per page they want to see.

Cookies are also used to track users across a website. Third-party cookies and Web bugs, explained below, also allow for tracking across multiple sites. Tracking within a site is typically done with the aim of producing usage statistics, while tracking across sites is typically used by advertising companies to produce anonymous user profiles, which are then used to target advertising (deciding which advertising image to show) based on the user profile.

Since their introduction on the Internet, misconceptions about cookies have circulated on the Internet and in the media like:

* Cookies are like worms and viruses in that they can erase data from the user's hard disks
* Cookies generate popups
* Cookies are used for spamming
* Cookies are only used for advertising

Typically, a cookie comprises:

* a name for the cookie (chosen by the Web site you are visiting)
* a value (unique number for the cookie) (determined by and stored by the Web site for future recognition and action)
* an expiration date
* a valid path (details about the Web page(s) that the visitor was on when the cookie was sent)
* a valid domain (the name of the Web site that created and can retrieve the cookie)
* a secure connection requirement (if the cookie is marked "secure", it will only be transmitted if the visitor is connected to a secure Web site

There are two types of cookies.

Session Cookies: These cookies reside on the Web browser and have no expiry date. They expire as soon as the visitor closes the Web browser. Session cookies remember information only for as long as the visitor operates the Web browser in a single "session" (or "sitting"). Session cookies can be used by Web site operators to determine information such as what parts of a Web site are popular, how long people stay on certain sections of a Web site and what browsers people are using.

Persistent Cookies: These cookies have an expiry date, are stored on a visitor's hard drive and are read by the visitor's browser each time the visitor visits the Web site that sent the cookie. It is possible for the Web site that created the cookie to extend the expiry date without notice to the visitor. They will remain there until the set date has expired or until the visitor has deleted the file. However, most people do not know how to delete cookies. In addition, the prolonged existence of persistent cookies means they can be used to follow Web browsing behaviour and purchasing habits. In some cases, they can also be used to identify a Web visitor when the persistent cookie data is combined with information from other sources such as databases (for example, matching an IP address with a person's name).

phoenixlipo
05-29-2009, 02:03 PM
Hi,

The PHP comment syntax always begins with a special character sequence and all text that appears between the start of the comment and the end will be ignored.

In HTML a comment's main purpose is to serve as a note to you, the web developer or to others who may view your website's source code. However, PHP's comments are different in that they will not be displayed to your visitors. The only way to view PHP comments is to open the PHP file for editing. This makes PHP comments only useful to PHP programmers.

thanks,

Sawa7
07-01-2009, 09:36 AM
PHP sessions actually create a cookie by default that saves the session by default? If this is the case, is there a way I can set how long to keep that session cookie for?


*EDIT* I just tried without setting the cookies, and after logging-in and closing and opening the browser, it did NOT remember my log-in. Is there something I need to do with the PHPSESSION cookie, like set it as the session ID or something?

*EDIT 2* Looking at my cookies through firefox, it removes the PHPSESSION cookie after the browser is closed (which is what I don't want) I want to keep the login active even after the user closes the browser. That is why I thought I should be setting the cookies manually with a longer expiration (3 days or so).