<?xml version="1.0" encoding="utf-8"?>
<tutorial>

<description>Learn how to add and play around with cookies.</description>
<keywords>tutorial, PHP, tutorials, cookies, storing, information</keywords>
<title>Cookies</title>

<slug>
Learn how to add and play around with cookies.
</slug>

<text>
Setting and playing around with cookies is a fun and useful way to save data on a user's hard drive, and can successfully store valuable information which may be helpful the next time they come to the site. Its fairly simple to set up, and even easier to read. To use it, you have to remember some guidelines:
<ol>
	<li>You have to put the cookie code as the first line of your file, it has to be before the head, before any other code, HTML or PHP.</li>
	<li>The cookie will not work on the page until its refreshed, or the user visits the page again. </li>
	<li>The cookie only works on the directory you're in and down. Its best to place the cookie in a file which is located in the root directory, so you can use the cookie anywhere around the site. </li>
</ol>
Here's the code to set a variable:
<![CDATA[
<pre>
&lt;? 
setcookie ("cookie", "spoono rocks");  
//makes a variable $cookie with a value of "spoono rocks"
?&gt;
</pre>
]]>

Now, the next time someone visits this page, or any other PHP page in the same directory, or a sub directory inside, the cooie variable will already be defined. If you echo out $_COOKIE["cookie"], it will display "spoono rocks".<br></br>Moving on, if you want to set a time on this cookie to expire after a a given time, just put it as the next field, with the amount of time you want it to last till in seconds. For example:
<![CDATA[
<pre>
&lt;? 
setcookie ("cookie", "spoono rocks", time()+3600);  
//same thing as above, but will last for 3600 seconds, or one hour from the current time.
?&gt;
</pre>
]]>

Some people also want to know how to store multiple variables. Not a problem, just make more cookies. Here is a code example, its not too hard to follow :) 
<![CDATA[
<pre>
&lt;? 
setcookie ("cookie1", "spoono rocks once");
setcookie ("cookie2", "spoono rocks 2 times");
setcookie ("cookie3", "spoono rocks 3 times");

//Then in the main body, you can place:

echo $_COOKIE["cookie1"]; //will display "spoono rocks once"
echo $_COOKIE["cookie2"];  // "spoono rocks 2 times"
echo $_COOKIE["cookie3"];  // "spoono rocks 3 times"

//if you want to display all cookies for debugging
//you can use:
print_r($_COOKIE);
?&gt;
</pre>
]]>

Finally, you may wonder how do you delete this cookie when you don't want it anymore? Well PHP has a solution for that too! Just place a new setcookie() statement with the variable you want to deconstruct. For example, if you want to deconstruct the "cookie" variable:
<![CDATA[
<pre>
&lt;? 
setcookie ("cookie");  
//deletes the spoono cookie
?&gt;
</pre>
]]>
Well, that should about cover everything you need to know about cookies. Not too tough was it? Well thats it folks. I hope it works out for you and if it doesn't, email for help at <link><url>mailto:webmaster@spoono.com</url>webmaster@spoono.com</link> and we'll try to help you out.
</text>

</tutorial>

