Using a web cookie and PHP to remember something useful!!
Freeola Internet Get Dotted Domains Chat & Gaming
Freeola Gaming
Easy Web Site Builder - Create Blogs, Galleries & More...
Freeola Games HomeChat ForumsCheatsWalkthroughsTips & TrophiesReviewsWin Free GamesMyFreeolainvader-bob
£50,000 Domain Giveaway. Get Your FREE .info Domain Name Today!
 
Browse Chat Forums:
 Chat Forums Home View Latest Post Chat Rules Chat Safety & Tips Top Posters
  Games Homepage  Win Free Games  Latest Winners  Hall of Fame  See Who's Online  Update Your Profile
  Free Web Site  Free Domain Hosting  Emergency Internet  Broadband Offers  Broadband Speed Test
 

Did you know...?

Help & Support 7 Days a Week

Win Amazon Vouchers!

Visit our Support Pages E-mail a Support Request Contact Us

Build Your Own Web Site In Minutes!

nothing
You Are Here Chat Home (95)   Web Development & Technical Chat  Using a web cookie a...
Just lurking around? Why not join in? You could win free games just by chatting. Choose your Nickname in MyFreeola or Sign Up Here.
 
 
 General Chat     Web and Technical Chat     Games Chat     Game Reviews   
 
Using a web cookie and PHP to remember something useful!!
"It goes so quickly"
Regular
on 29/07/2009 at 8:04:38PM
Total Posts: 460
Original Post:
Using a web cookie and PHP to remember something useful!!

Even in this day an age, the Internet is still based on a “stateless network”, which means each and every time you view a web page, the Internet thinks it is a fresh request from someone new. It doesn’t remember anything about any previous web pages you’ve looked at, and neither does it care. All the Internet does is ferry web traffic about, from one computer to another.

Yet, you’ll notice that while browsing the web, you’re often remembered, either from a search term you’ve Binged, an item you’ve already looked at on Amazon, or a web form you’ve filled in. Even while browsing Freeola, you’ll notice it noticing you. If you want to reply, you don’t have to log in each time, even though the Internet doesn’t know you’ve logged in!

This is because of your web browser, and its ability to be told to remember things.

Maybe you’ve heard of the humble cookie before, and it’s no surprise if you have. The little cookie jar on your computer is there to keep a little post-it note of information that any web site owner can ask it to! It could be a name, an ID, a colour, anything!

What is a web cookie?

A web cookie, often referred to as an HTTP cookie or Internet cookie, is a single text file that web browsers use to store a small amount of text. This text is placed their by web sites, and is sent back to the web site each time a new “request” is made. This means that if Freeola set a cookie with the text “Hello, from Freeola!”, then each time you browsed to a different web page within the freeola.com domain name, that text from within the cookie would be sent back to the Freeola web site as well.

Why do web sites use cookies?!

Web Sites have use cookies for a long time. They are used to remember a user who has looked in, keep track of items a user has added to a shopping cart, remember a preference setting, or even just to know if a user has already visited a particular web page.

Amazon, for example, creates a cookie to identify a shopping basket, so if you browse Amazon and add an iPod Touch and Bolt Blu-Ray to your shopping basket, Amazon creates a shopping cart on its web servers, and gives it a unique ID, and this unique ID is stored in the cookie sent to your web browser. Each time you browse Amazon, this unique ID is sent to Amazon, and they know which shopping basket is yours. If the cookie isn’t deleted, or doesn’t expire, then even shutting down the PC and coming back in a day should display the same shopping basket.

How might I use one?

For the same reasons as other web sites, you might want to set a cookie to increase your web site interactivity, remember that a user has seen the page already, remember that the user likes your yellow CSS design, or indeed any number of different reasons. The key feature of the cookie is to store small piece of text, in what are called name / value pairs. If you have something that you’d like stored on the users browsers, and retreated each time a user requests on of your web pages, then the cookie might be for you!

Are they safe and secure?

A cookie is a plan text file, similar to how Windows Notepad text files are stored. Because of this method of storage, cookies can’t be used to run programs on your computer, or sniff for your email address or passwords from other web sites.

However, as a cookie is a plain text file stored on a users PC, they shouldn’t be considered “safe” to store certain types of information, in that you should not use them to store a users password, credit card number, or other sensitive information. This is because they can be easily viewed on the users computer, and can also transmitted using an unsecured standard Internet connection.

Can I eat a cookie while installing my new Broadband Accelerator?

As that is a different type of cookie, you’ll have to ask Hmmm… about eating a cookie while working on your Broadband Accelerator installation! Hmmm… likes people to concentrate on their work when it comes to messing about with your phone socket, so he might not let you have a cookie while you’re improving your ADSL connection! And of course, getting any crumbs in your socket could affect your download speeds at peak times!

How can I create a cookie for my web site?

A cookie can be created in a number of ways, either client side (by the web browser), such as with JavaScript, or server side (by the web server) using, for example, PHP.

We’ll be using PHP within this article, as it’s made easier with dedicated functions, and it perhaps more usable and functional. Because of this, you’ll need to already be ever so slightly familiar with PHP before going any further.

Planning ahead – what will I store and why?

Before creating any cookies, you should think carefully about what you want to store, and how you’ll use that stored information later. Because this is a short and rather basic introduction, any cookie we set will be quite bland, but hopefully you’ll be able to build up something useful from it.

You said name / value pairs earlier, so, what’s that then?

If you’ve used PHP before, or even any programming or scripting language, you should be familiar with the concept of variables that store data, and are given a name to reference the data from. The cookie works in a similar way, allowing you to create a variable name, and giving it a value. You can have multiple names and values within your cookie, allowing you to store multiple piece of information.

Creating a cookie!

To create a cookie, PHP provides a function called setcookie, that adds the code to your web site that will create a cookie, and sends it back to the users web browser when they request a web page. All that is needed is a variable name, and value to store in the cookie. So for example, if we wanted to create a cookie, we could use the code:

    <?php setcookie("myCookie","This is my first Cookie"); ?>


Remember, to use PHP, you’ll need to make sure your web page has an extension of .php, or .php5 (recommended).

The code above will send an instruction to your web browser to create a new cookie for your web site, and to store the name / value pair of “myCookie” (the variable name) and “This is my first Cookie” (the value), and this data will be stored on the users PC.

It is important that you include the setcookie function call before anything is displayed in the web browser, as the cookie code is included within the HTTP Headers. If you had the code:

    <html>
    …
    <?php setcookie("myCookie","This is my first Cookie"); ?>
    …
    </html>


… or …

    <?php
    echo "<p>hello</p>";
    setcookie("myCookie","This is my first Cookie");
    ?>


... you would find a PHP error message appears, telling you the “headers have been sent”. The setcookie function must come before any of this.

You can use “output buffering” to work around that, but I won’t go in to that now, as this is just a basic intro to cookies.

If you wanted to create multiple name / value pairs of data, you can use multiple setcookie function calls, one after the other:

    <?php
    setcookie("myCookie","This is my first Cookie");
    setcookie("anotherCookie","This is my second Cookie");
    setcookie("aThirdCookie","This is my third Cookie");
    ?>


… ensuring you use a unique name for each value you want to store, otherwise the last entry will be the only one stored, and all the others will be over-written.

Deleting a cookie!

To delete a cookie, you can use the setcookie function again, but this time set an empty value, and this will overwrite the old cookie, and the web browser will delete it (as they tend not to store empty cookies):

    <?php
    setcookie("myCookie"," ");
    setcookie("anotherCookie"," ");
    setcookie("aThirdCookie"," ");
    ?>


Reading a cookie!

With a cookie being created, each time the user either refreshes the page, or browses another page under the same domain name, the cookie name / value pair are sent back with each request, and PHP will automatically catch the cookie for you within a global variable, which is called $_COOKIE. This global variable is actually all the name / value pairs that are stored in the cookie, and you can access them all individually by referencing the “name” of the value you’re after, using the syntax $_COOKIE["name"].

In our above example, we used the name “myCookie”, so to access the value we stored under this name, we would use the code:

    <?php echo $_COOKIE["myCookie"]; ?>


… which would present our cookie value on screen.

Want to get baking?

I’ve thrown up a live example (or the source code) that will set a single cookie with a name of “freshPHPCookie” and a value of “mmmmmm cookie!!”.

A Cookie can be extended in a number of ways, which include allowing you to set which domain name and web folder the cookie is sent and received from, and when the cookie expires, which could be an hour, a day, etc.

But, for now, as this is just a basic “look, cookies exist” type of article, feel free to give cookie baking a go, but don’t burn yourself! Head on over to the official PHP manual to read all about the setcookie function

= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =

As always, any comments, questions, and especially corrections are welcome.
View More Threads Post a Reply  
Displaying 1 - 1 of 1 Replies:
LukeM
"Imperfection"
Staff
on 30/07/2009 at 11:42:10AM
Edited: 30/7/09 11:42
Total Posts: 70
Mmmm my favorite kind of cookies. Sometimes a more foolproof way of deleting a cookie is to set it's expiry date to something in the past:

//create cookie
setcookie("contents","chocchip");

//delete cookie
setcookie("contents","",time()-3600);
 
Your Details MyFreeola Internet Settings Control Panel Your Details
Login or create a free account.
Forgotten your password?
Free Account Sign-Up
 
Your Details
Search
 
 
 
Fantastic FREE Unlimited Services for every freeola internet customer in the UK!
Register Domain Names. Buy from £2.99
e.g. yourcompany.com
or just yourcompany.
MORE ABOUT DOMAIN NAMES