PHP AND COOKIES; A GOOD MIX!

Introduction Cookies hit daylong been utilised in PHP scripts,
and are a rattling multipurpose function. But what meet are cookies?
Maybe you hit utilised then, but you ease don’t undergo meet what
they are. Or you are completely newborn to cookies? It doesn’t
matter, because in this tutorial I power exhibit you meet what
cookies are, and what they are utilised for.

Cookies in a nutshell Cookies are diminutive pieces of
information that is stored on the machine of your visitors.
Each application handles it differently, but most only accumulation the
information in a diminutive book file. cyberspace Explorer has a
special folder, which crapper be institute in your C:Windows or
C:WindowsSystem32 folder. You crapper withdraw every your cookies, by
going to the Options and ‘Clearing Cookies’ or deleting them by
hand. I don’t propose this though.

Almost every website uses cookies. If you go to Amazon.com, you
will intend individual cookies. The aforementioned goes for CNN.com. Even Google
uses cookies! They are extremely multipurpose for (temporarily)
storing information. For example, if you hit a login grouping for
your visitors, you could spend their userid and countersign (very
heavily encrypted!) so they are automatically logged in the next
time they meet your website.

Or you could advert their terminal visit, and particular everything
that is new. And that’s meet the beginning.

Using Cookies Using cookies in PHP is extremely easy. In
fact, there is null to it, because of PHP’s integral setcookie() function. Have a
look at the documentation, and then essay the mass example:

<?php

// Set a cake // Cookie name: study // Cookie value: Dennis
Pallett // Cookie expire: in 24 hours

setcookie (’name’, ‘Dennis Pallett’, time() + (60*60*24)); ?>

If you removed the cipher above, then a cake power be set. That’s
all. The cake study and continuance are pretty obvious. The cookie
expire is when the cake expires, or goes away. Simply ingest the
time() duty and add the
number of seconds you poverty to hit the cake acquirable to it.
In the warning I added 60*60*24=86400 seconds, or 24 hours.

If you hit looked at the documentation, you belike noticed
there are added arguments. As the substantiation says, the
path is to bounds a cake to a limited line on your scheme server.
This is ofttimes utilised when you removed binary instances of the same
script in removed directories. You crapper safely omit this
argument when it doesn’t concern if the cake is available
site-wide.

There is also the field argument. This crapper be utilised to bounds the
cookie to a limited sub-domain, e.g. test.example.com. You can
also safely cut this argument, or ordered it to .example.com
(note the prototypal period, this is essential!).

Finally, there is also the bonded argument. This discussion is
only utilised for cookies that are dispatched over a bonded HTTPS
connection (SSL). Just cut this argument, unless you’re
working with a bonded connection.

One abstract that should be mentioned is that cookies staleness be set,
before you pass some HTML/text. It’s belike prizewinning if you turn
on production buffering by swing ob_start() at the crowning of your
page.

Now that you hit ordered a cookie, you belike poverty to retrieve
the continuance as well. After all, that is the full saucer of using
cookies. Thankfully, as PHP is ever so easy, you crapper retrieve
the aforementioned artefact as you regain a GET value. See the following
example to regain the continuance of the preceding example:

<?php reflexion ‘Your study is ‘ . $_COOKIE[’name’]; ?>

This should indicant "Your study is Dennis Pallett".
There’s null more to it. It’s meet that easy!

Finally, digit abstract you belike poverty to do as substantially is remove
cookies. This is as cushy as environment them. Simply modify the
value of the cake to FALSE, and modify the suspire fellow to
-3000 seconds. See the mass example:

<?php setcookie (’name’, FALSE, time()-1000); ?>

Checking if cookies are enabled Before you move using
cookies, you staleness attain trusty your traveller has cookies enabled.
This crapper be finished with a only PHP checking script.
Unfortunately, the PHP tender needs to charge to analyse for
cookies. But this crapper be finished rattling transparently, and your
visitor should scarce attending anything.

The mass warning power prototypal ordered a effort cookie, then reload
the page, and eventually analyse whether cookies are enabled.

<?php error_reporting (E_ALL ^ E_WARNING ^ E_NOTICE);

// Check if cake has been ordered or not if ($_GET[’set’] !=
‘yes’) { // Set cake setcookie (’test’, ‘test’, time() + 60);

// Reload tender brick ("Location:
checkcookies.php?set=yes"); } added { // Check if cookie
exists if (!empty($_COOKIE[’test’])) { reflexion "Cookies are
enabled on your browser"; } added { reflexion "Cookies are
<b>NOT</b> enabled on your browser"; } } ?>

Run the cipher above, and wager what the production is. Check if cookies
are enabled in your browser. If they’re not enabled, then you
can enable them by feat to your browser’s options.
Unfortunately, this is assorted from apiece browser, so I can’t
give you literal instructions. But Google can.

Storing Arrays One feature of cookies that is often
missed in articles is the knowledge to news arrays. Cookies can
be utilised to accumulation multi-dimensional arrays, which crapper be
extremely multipurpose to accumulation data.

Consider the mass code;

<?php setcookie ("name[first]", "Dennis",
time() + (60*60*24)); setcookie ("name[last]",
"Pallett", time() + (60*60*24)); ?>

You crapper then pass these digit cookies using the mass code:

<?php reflexion "First Name: " .
$_COOKIE[’name’][’first’]; reflexion "<br />Last Name:
" . $_COOKIE[’name’][’last’]; ?>

The cake ‘name’ is an array, and has binary values. You can
even go deeper and hit multi-dimensional arrays, e.g.
$_COOKIE[’name’][’test’][’something’][’value’]. You could store
whole arrays of accumulation in cookies. But watch that you don’t store
too such data, there are destined filler limits to cookies.

In Conclusion… Cookies are rattling versatile, and crapper be
used for a aggregation of assorted purposes. Many websites ingest cookies,
and cookies crapper rattling attain your website more personalized.
Using cookies in PHP isn’t hornlike at all, and you should be able
to ingest them without some difficulty.

Before actively using cookies in your website, you staleness check
whether the traveller has enabled them in their browser. If they
don’t hit cookies enabled, you staleness either direct to a
non-cookies edition of your website, or you crapper attain trusty your
website also entireness without cookies.

You crapper download a distribution playscript at h
ttp://www.phpit.net/demo/php%20and%20cookies/logger.zip,
where cookies are utilised in a (somewhat) applicatory way. In this
example, there is a logging module, titled log.php and a display
module, titled history.php. Basically, you allow the log.php
in another PHP pages, and then you crapper analyse history.php to lookup
all the pages you hit viewed and how often. The warning uses
arrays, and stores them in cookies.

The examples in this article crapper be downloaded at http://www.phpit.net/demo/php%20and%20cookies/examples.zip.

If you hit a rattling unequalled applicatory artefact of using cookies,
please permit me undergo at dennis [AT] nocertainty [DOT] com. I’d
really same to center most engrossing structure of using cookies.

Comments are closed.