
<?
setcookie ("cookie", "spoono rocks");
//makes a variable $cookie with a value of "spoono rocks"
?>
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".
<?
setcookie ("cookie", "spoono rocks", time()+3600);
//same thing as above, but will last for 3600 seconds, or one hour from the current time.
?>
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 :)
<?
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);
?>
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:
<?
setcookie ("cookie");
//deletes the spoono cookie
?>
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 webmaster@spoono.com and we'll try to help you out.
Copyright © 2000-2008 Spoono, LLC. All rights reserved.
Network: Reseller Web Hosting by Spoono Host | Spoonloads | Absolute Cross
Terms of Service | Privacy Policy.