Jump to content
MakeWebGames

[FAQ] Basic Cookies


hamster01

Recommended Posts

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:

Link to comment
Share on other sites

  • 1 month later...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...