In this lesson our instructor talks about cookies. First, he discusses what is a cookie, HTTP, set-cookie header, and setcookie(). Then he talks about cookies attributes, $_COOKIE Superglobal, and security implications. He ends the lesson with seven helpful homework challenges.
A cookie is a piece of information set by an HTTP server and sent to a web client within an HTTP
response for storage by the client.
Cookies are associated with a particular website, and each time a client requests a page from a website for
which it has received & stored a cookie, it includes the corresponding cookie’s data within its HTTP
HTTP is a stateless protocol meaning that each HTTP transaction between a client & server has no
‘memory’ of any previous transactions.
The purpose of cookies is to allow state, or knowledge of previous events, to be introduced into HTTP
The way a web server sends a cookie to a client is via the Set-Cookie header of an HTTP response:
HTTP/1.1 200 OK
Date: Wed, 3 Aug 2011 23:59:59 GMT
Cookie data is in the form of name-value pairs.
Each time a client requests a page from a website for which it has a cookie, it sends the cookie’s
information back to the server using the Cookie HTTP request header:
GET /index.html HTTP/1.1
setcookie() is a built-in PHP function that allows a script to send a cookie to a web client. It
must be called before any of a script’s output is generated.
Cookies have several attributes that can be set which determine whether a client sends a particular cookie in
its request for a page from a particular website:
domain – specifies the domain which a cookie applies to
path – specifies a particular path within a domain that the cookie applies to
expires – the data & time when a cookie is no longer valid
$_COOKIE is a superglobal associative array containing any cookie name/value pairs sent by a
client in its request for a PHP script.
Lecture Slides are screen-captured images of important points in the lecture. Students can download and print out these lecture slide images to do practice problems as well as take notes while watching the lecture.