Website Development Using PHP & Mysql: Fcait BCA
Website Development Using PHP & Mysql: Fcait BCA
Website BCA
Development using
PHP & MySQL
Unit - 4
Cookies
2
PHP Cookies
A cookie is often used to identify a user.
A cookie is a small fle 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, we can both create and retrieve
cookie values.
PHP transparently supports HTTP cookies.
PHP Cookies
Definatioit
An HTTP cookie (also called web cookie,
Iiteriet cookie, browser cookie, or
simply cookie) is a small piece of data
sent from a website and stored on the
user's computer by the user's web
browser while the user is browsing..
PHP Cookies
PHP Cookies
There are three steps involved in identifying returning users.
Server script sends a set of cookies to the browser.
For example name, age, or identifcation number etc.
Browser stores this information on local machine for
future use.
When next time browser sends any request to web
server then it sends those cookies information to the
server and server uses that information to identify the
user.
How cookie works?
Uses of Cookies
●
Sessioi mnainagemeitt Cookies are widely used to manage user
sessions. For example, when you use an online shopping cart, you keep
adding items in the cart and fnally when you checkout, all of those items
are added to the list of items you have purchased. This can be achieved
using cookies.
●
User ideitifcnatioit Once a user visits a webpage, using cookies, that
user can be remembered. And later on, depending upon the search/visit
pattern of the user, content which the user likely to be visited are served.
A good example of this is 'Retargetting'. A concept used in online
marketing, where depending upon the user's choice of content,
advertisements of the relevant product, which the user may buy, are
served.
●
Trnackiig / Ainalyticst Cookies are used to track the user. Which, in turn,
is used to analyze and serve various kind of data of great value, like
location, technologies (e.g. browser, OS) form where the user visited, how
long (s)he stayed on various pages etc.
Cookie Anatomy
●
Cookies are usually set ii nai HTTP henader.
●
A PHP script that sets a cookie might send headers that look
something like this −
HTTP/1.1 200 OK
Date: Fri, 04 Feb 2000 21:03:38 GMT
Server: Apache/1.3.9 (UNIX) PHP/4.0b3
Set-Cookie: name=xyz; expires=Friday, 04-Feb-07 22:03:38 GMT;
path=/; domain=abc.com
Connection: close
Content-Type: text/html
Cookie Anatomy
●
As you can see, the Set-Cookie henader
contains a name value pair, a GMT date, a
path and a domain. The name and value will
be URL encoded.
●
The expires feld is an instruction to the
browser to "forget" the cookie after the given
time and date.
●
If the browser is confgured to store cookies, it
will then keep this information until the expiry
date.
Create Cookies With PHP
?>
</body>
</html>
isset() function
You can use isset() function to check if a cookie is set or not.
<html>
<head>
<title>Accessing Cookies with PHP</title>
</head>
<body>
<?php
if( isset($_COOKIE["name"]))
echo "Welcome " . $_COOKIE["name"] . "<br />";
else
echo "Sorry... Not recognized" . "<br />";
?>
</body>
</html>
Retrieving the Cookie value
<?php
print_r($_COOKIE); //output the contents of the cookie array
variable
?>