GerardWay Posted September 2, 2015 Share Posted September 2, 2015 (edited) 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 September 2, 2015 by GerardWay Quote Link to comment Share on other sites More sharing options...
requinix Posted September 2, 2015 Share Posted September 2, 2015 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. Quote Link to comment Share on other sites More sharing options...
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.