hamster01 Posted May 18, 2007 Posted May 18, 2007 In this tutorial you will learn how to: Make Cookies. Use Cookies. Check for of Cookies. Delete Cookies. Remember: You cannot put Any html code before the code of the cookies! Firstly you must learn how to set a cookie. The order of it is: setcookie(name, value, expire);. example: $time = time(); $text = "This is what the cookie will hold"; setcookie(Name, $text, $time+3600); And that is how you set a cookie! Now to call a cookie to a script: You can use: echo "Your Cookies Value: ".$_COOKIE[Name]; That will then output: Your Cookies Value: This is what the cookie will hold You can also use it to check for values. if (isset($_COOKIE[name])){ echo "You have a cookie!"; } else { echo "You have no cookies!"; } That will either return TRUE, if you have a cookie or FALSE if you dont. Now you know how to use cookies. Now onto deleting cookies. Its a good idea to check if there is a cookie first before deleting. Example: $time = time(); if (isset($_COOKIE[name])){ setcookie(Name, '', $time-3600); } else { echo "You have no cookies to delete!"; } It may look like we are making another cookie, but we are just adjusting to cookie to expire immediately! You just you - to set the cookie back to expire. Now we will make a test script using what you have just learned. We open the script and then check if the form is posted. if the post is "Submit" create a cookie, else if the post is "Reset" be delete it. <?php if ($_POST['Submit']){ setcookie(id,'kis works',time()+3600); echo "Cookie Made!"; } if ($_POST['Reset']){ if (isset($_COOKIE[id])){ setcookie(id,'',time()-3600); echo "Deleted!"; } else { echo "You have no such cookie to delete!"; } } Now we will make a form with two buttons. echo " "; Now we will display the cookie's value if it exists. echo "Previous Attempts: "; if (isset($_COOKIE[id])){ echo $_COOKIE[id]; } else { echo "None"; } Now we close the script. ?> You now know how to make, use and delete cookies! The complete script: <?php /* >> Basic Cookies Tutorial */ if ($_POST['Submit']){ setcookie(id,'kis works',time()+3600); echo "Cookie Made!"; } if ($_POST['Reset']){ if (isset($_COOKIE[id])){ setcookie(id,'',time()-3600); echo "Deleted!"; } else { echo "You have no such cookie to delete!"; } } echo " "; echo "Previous Attempts: "; if (isset($_COOKIE[id])){ echo $_COOKIE[id]; } else { echo "None"; } ?> Hope This Helps some people. :wink: Quote
Aqua Posted May 18, 2007 Posted May 18, 2007 Re: Basic Cookies Good stuff ... many people wonder about this :lol: Quote
$$ ?????? $$ Posted July 17, 2007 Posted July 17, 2007 Re: Basic Cookies lol i learnt a few things their and alot of people probably will aswell :-) Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.