Setting and Removing Cookies

Hello guys, I’m a beginner at PHP.

I have created cookie.php:

<!DOCTYPE html> 
 
<?php

if(!isset($_COOKIE['myCookie'])){
    $welcome = "Hello New Guy!";
    setcookie('myCookie', 1, time()*7*365);
}
else{
    $welcome = "Welcome back!";
}

?>
 
<html>
<head>
	<meta charset="utf-8">
	<title>Cookie.php</title>
</head>

<body>
    
    <div>
        <?php if(isset($welcome)) echo $welcome; ?>
        <a href="removeCookie.php">Remove Cookie</a>
    </div>
    
</body>
</html>

Note: There is an anchor to removeCookie.php right below the div.

Here is the removeCookie.php:


<?php

setcookie('myCookie', 1, time() - 3600);
header("location: cookie.php");

?>

Can anyone spot the error?

Could you explain what the issue is that your having?

Likewise, not sure what the Q is, but this line is surely wrong:


setcookie('myCookie', 1, time()*7*365);

SB


setcookie('myCookie', 1, time() + (86400*365) );

This is if the idea is to add one year of life to the cookie.

The idea being

take the number of seconds up to now - time()
to that add +
the number of seconds in a a day - 86400
and multiply by - *
the number of days in a year - 365

Hi guys. Sorry for not being clear.

This just doesn’t work. The output is “Hello New Guy!” and remains so, regardless of how many times I refresh the page.

Is this because the cookie has not been set, or perhaps I am not receiving the cookie correctly?

It seems that as soon as you delete the cookie, you send the user back round to get another one so the condition is always met.

If I click on “delete cookie”, why are you sending me back to get another?

Why don’t you tell us what you want to happen, fill the idea out a bit - then someone can help you.

Hi Cups,

Thanks for reply. I am now learning about the behaviour of cookies, and I am experimenting.

I am following the screencast tutorial by Jeffrey Way: In the Woods – Diving into PHP: Day 9

This is what I was hoping to do with the first block of code:


if(!isset($_COOKIE['myCookie'])){
    $welcome = "Hello New Guy!";
    setcookie('myCookie', 1, time() + (86400*365));
}
else{
    $welcome = "Welcome back!";
}

  1. Check if there is an existing cookie ‘myCookie’ in the browser.
  2. If myCookie DOES NOT exist, then the $welcome = “Hello New Guy!”, and a cookie will be set.
  3. If myCookie DOES exist then the $welcome = “Welcome Back!”

As you can see, the $welcome variable has two different values depending on whether the cookie exists or not. And I output the $welcome as such:


    <div>
        <?php if(isset($welcome)) echo $welcome; ?>
        <a href="removeCookie.php">Remove Cookie</a>
    </div>

I have also linked to the page removeCookie.php, where the cookie may be removed:


<?php
setcookie('myCookie', 1, time() - 3600);
header("location: cookie.php");
?>

Have you tried visiting the page, and then checking your cookies to see if you did, in fact, get the cookie?

how about you put this as the first line in PHP


<?php

if( isset( $_COOKIE )){
var_dump( $_COOKIE );
}

// then do your conditional checks...
?>

var_dump ($_COOKIE); returns the following


array(1) { ["SQLiteManager_currentLangue"]=> string(1) "2" }

So I guess there is, in fact, a cookie in existence?

Well when you refresh the page, it should show your cookie in there (alongside that one you showed us) when you visit a page after you have deleted it, it should not be there.

To focus in on this issue, create 3 different pages, 1,2,3.

1 sets the cookie
2 reads the cookie
3 deletes the cookie

All with var_dump() at the top of the page, also depending on your browser install some “developer tools” so you can see the content of any given cookie at any time.

Try this tutorial if you can make no headway, and listen, cookies can be a hard thing to grasp, I remember trying to dig them when I first encountered Javascript - took me flippin’ ages.

Once you get it it is easy to work out what you are doing wrong. (Having said that, be warned that localhost is not the same host as 127.0.0.1 and I have run into issues with IE not reading cookies set on localhost, 'twas years ago though)