Jump to content

Cookies Not Working


GerardWay

Recommended Posts

I have a button which I want a specific output for after being clicked once and a specific output after being clicked twice. How can I do this? I tried to use cookies.


<!-- This is in index.php -->
    <a href="action.php"><button class="btn">Click</button></a>

 

I want it to set the cookie 'clicked_once' after it has been clicked once and then go back to the button page after three seconds:


<?php

// Check if the 'clicked once' cookie has already been set
if(!isset($_COOKIE['clicked_once'])) {
// If not, set it
setcookie('clicked_once', getIP());
?>
           
<div class="notice">
<p>You have clicked the button once. <a href="javascript:history.go(-1)">Go back to click it again</a> </p>
</div>
           
<?php
}
?>

 

Then, once the user has clicked the button again, I want to set the *'used_twice'* cookie (which expires after an hour) and give them the result:

 



<?php

// Check if the 'clicked once' cookie is set...
// ... and if the 'clicked twice' one hasn't
// If so, unset the 'clicked once' cookie and set the 'clicked twice' one
// And then give them the result


if(isset($_COOKIE['clicked_once']) && !isset($_COOKIE['used_twice'])) {
   unset($_COOKIE['clicked_once']);
   setcookie('clicked_twice', getIP(), time()+3600);
?>
<div class="notice"><p>Success!</p></div>
<?php
}
?>


 

After that and they try to click the button again I want it to give an error message:

 



// If the button has been clicked twice then provide no result 
<?php if(isset($_COOKIE['clicked_twice'])) { ?>
   <div class="notice"><p>You can only do this once every hour. Please try again later.</p></div>
<?php } ?>


 

This is not working though. Once you click the button on index.php, here is the output:

 



Warning: Cannot modify header information - headers already sent by (output started at getMem.php:65) getMem.php on line 69
You have clicked the button once. Go back to click it again.


 

When you click the button again, you get the same output. Here is the output I want from clicking the button once:

 



You have clicked the button once. Please allow the page to refresh and click it again.


Here is the output I want from clicking the button twice:



Here is your code: {code pulled from list}


I also checked my browser and no cookies had been set.

Edited by GerardWay
Link to comment
Share on other sites

If that "Cannot modify header information" error message was referring to the setcookie() line then that's your problem: you can't call it after there has been any output. Rearrange your logic so that you set the cookie before beginning output.

 

Oh, and your code says "clicked_twice" in some places and "used_twice" in others.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.   Restore formatting

  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...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.