HTTP is, by itself, a stateless protocol, which means that it
"has no memory". This, in turn, means that as you browse from one page to
another, there is no "connection" of any kind between the last page and
the current page, or between the current page and the next page you go
to.
This state of affairs led to problems with, among other things,
on-line commercial activities like keeping track of what's in a buyer's
"shopping cart", for example.
There are essentially two widely used methods of giving our web pages
some memory:
- Cookies
- A cookie is a small file (often text but now, in Firefox
at least, an SQLite file) that can be sent from a web page to a browser
and stored on a client machine. When the browser later returns to that
web page, the browser will send that cookie to the server hosting that
web page, where the information in the cookie can be used to
"personalize" the visit.
- Sessions
- A session is a file on the server identified by a cookie
ID reference sent from the client.
Advantages of cookies:
- Can have a long lifespan, so your data "works" when the client
comes back the next day.
- Can be accessed on the client side via JavaScript.
- Also works if you have a server "cluster".
Disadvantages of cookies:
- Can be altered by the user.
- Limited in size (4kb).
Set a cookie with the setcookie()
function, which has six
parameters:
- string name (name of the cookie)
Cookie access: $HTTP_COOKIE_VARS (old variable), but now use
$_COOKIE["name"]
- string value (value of the name variable)
- int expire (expiration time)
-- no time ==> expiry when browser closes
-- time()+86400 ==> 1 day
- string path (sets the path to a directory where the cookie will be
active)
-- "/" ==> all files on the server (this is the default)
-- "/some-directory/" some-directory and its subdirectories
- domain
-- Cookies are valid only for host and domain that set them.
-- Example: mail.yoursite.com makes it available there but not
for
www.yoursite.com (for e.g.), but .yoursite.com ==> everywhere
- int secure
-- 0 ==> http or https
-- 1 ==> https only
Advantages of sessions:
- Cannot be altered by the user, so much more secure.
- Good for transient data, like an on-line shopping session.
- Data doesn't need to be transmitted with each page.
- Can be any size you want.
- Saves on bandwidth because only ID reference code passes from
client to server.
Disadvantages of sessions:
- Do not work across a server "cluster", unless you take extra steps,
like storing your session information in a database.
Notes
- The first parameter of setcookie() is required, the rest are
optional, and only the first three are used most of the time.
- Browsers can be set to reject or filter cookies.
- A single host can request acceptance of up to 20 cookies to be
stored on a user's browser.
- Cookies are "header data", as opposed to "body data", and since
HTTP requires all header data to be sent before any body data, page
cookies must be sent before any part of the page body.
- Cookies are sent to the server each time a user visits a page, so
if a cookie is set in a script, it does not become available till your
user visits the next page (or hits refresh).
- Cookies can be cleared manually by choosing a browser option or by
deleting files (if you know where they live).
- The best way to delete a cookie is to use the same
setcookie() call that created it, except for a time that is
in the past. A call to setcookie() with just the name
parameter set is supposed to work as well, but may not always do the
job.