GetDotted Domains

Viewing Thread:
"Using a web cookie and PHP to remember something useful!!"

The "Freeola Customer Forum" forum, which includes Retro Game Reviews, has been archived and is now read-only. You cannot post here or create a new thread or review on this forum.

Wed 29/07/09 at 20:04
Regular
"It goes so quickly"
Posts: 4,083
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("[B]myCookie[/B]","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("[B]myCookie[/B]","This is my first Cookie"); ?>
�
</html>


… or …

<?php
echo "<p>hello</p>";
setcookie("[B]myCookie[/B]","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("[B]myCookie[/B]","This is my first Cookie");
setcookie("[B]anotherCookie[/B]","This is my second Cookie");
setcookie("[B]aThirdCookie[/B]","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("[B]myCookie[/B]"," ");
setcookie("[B]anotherCookie[/B]"," ");
setcookie("[B]aThirdCookie[/B]"," ");
?>


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["[B]name[/B]"].

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["[B]myCookie[/B]"]; ?>


… 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.
Thu 30/07/09 at 11:42
Regular
"Embrace the Martian"
Posts: 285
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);
Wed 29/07/09 at 20:04
Regular
"It goes so quickly"
Posts: 4,083
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("[B]myCookie[/B]","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("[B]myCookie[/B]","This is my first Cookie"); ?>
�
</html>


… or …

<?php
echo "<p>hello</p>";
setcookie("[B]myCookie[/B]","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("[B]myCookie[/B]","This is my first Cookie");
setcookie("[B]anotherCookie[/B]","This is my second Cookie");
setcookie("[B]aThirdCookie[/B]","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("[B]myCookie[/B]"," ");
setcookie("[B]anotherCookie[/B]"," ");
setcookie("[B]aThirdCookie[/B]"," ");
?>


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["[B]name[/B]"].

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["[B]myCookie[/B]"]; ?>


… 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.

Freeola & GetDotted are rated 5 Stars

Check out some of our customer reviews below:

I am delighted.
Brilliant! As usual the careful and intuitive production that Freeola puts into everything it sets out to do. I am delighted.
Just a quick note to say thanks for a very good service ... in fact excellent service..
I am very happy with your customer service and speed and quality of my broadband connection .. keep up the good work . and a good new year to all of you at freeola.
Matthew Bradley

View More Reviews

Need some help? Give us a call on 01376 55 60 60

Go to Support Centre
Feedback Close Feedback

It appears you are using an old browser, as such, some parts of the Freeola and Getdotted site will not work as intended. Using the latest version of your browser, or another browser such as Google Chrome, Mozilla Firefox, or Opera will provide a better, safer browsing experience for you.