0

This code was working until about 30mins ago and now it suddenly ceases to work. It is simple and I am completely unable to see what is wrong with it or anything else that could be the reason for it not working. Can somebody help?`

<?php   
$cookie_name = "Name";
$cookie_value = "Value";
if(!isset($_COOKIE[$cookie_name])) {    
    setcookie($cookie_name, $cookie_value, time() + 10, "/"); 
    include 'popup.html';           
}
?>

What should happen is that it pops up a small Ad for users and then ads a cookie to stop it from popping up again. I had it set to expire after 10 seconds for testing but even if I set it to one week, the popup always appears. Like I mentioned before, it was working. Perfectly.

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
Ricky Payne
  • 71
  • 1
  • 12
  • is that the only thing on the whole page? –  Oct 06 '16 at 19:45
  • 2
    Check your log for "Headers already sent" warning. If you see it, see http://stackoverflow.com/questions/8028957/how-to-fix-headers-already-sent-error-in-php for how to resolve it. – Barmar Oct 06 '16 at 19:46
  • done any basic debugging, like checking for "headers already" sent error, firing up your browser's debug console to look at the response headers to see if the cookie's even there, etc...? – Marc B Oct 06 '16 at 19:46
  • _This code was working until about 30mins ago_ So you really spent time looking for a solution for yourself then..... at least 5 minutes! – RiggsFolly Oct 06 '16 at 19:48
  • The thing is that I really don't know why this is happening. No this isn't the only thing on the page. This is some PHP which is placed just after the tag and includes a html file with a popup ad. I have checked to see if the cookie was included in the header etc and nope, nothing is happening on that front. – Ricky Payne Oct 06 '16 at 19:52
  • Your codes fine so suspect its a browser issue. Have you tried a different browser without add-ons? – Kitson88 Oct 06 '16 at 19:56
  • If its after the `` then you already started sending to the browser and its too late to send cookies. _This code was working until about 30mins ago_ ___and then you changed something relevant obviously___ – RiggsFolly Oct 06 '16 at 19:57
  • That's the bizarre thing. I tried chrome and firefox and Edge and they all have the same problem. :/ – Ricky Payne Oct 06 '16 at 19:57
  • @RiggsFolly PHP doesn't send cookies. That isn't how it works, and the code was working fine as mentioned before. – Ricky Payne Oct 06 '16 at 19:59
  • 1
    Thats the point, as soon as you send even a space to the browser, i.e. any HTML like `` [as you say here](http://stackoverflow.com/questions/39904429/why-wont-the-following-script-add-a-cookie-to-the-browser-anymore?noredirect=1#comment67094478_39904429) and obviously the rest of the HTML that must come above a `` tag, you have lost your chance to send cookies – RiggsFolly Oct 06 '16 at 20:02
  • @RickyPayne He is spot on, I didn't realise that you placed this after the `` tag. Needs to be placed before the `` as angry RiggsFolly said :-P http://php.net/manual/en/function.setcookie.php – Kitson88 Oct 06 '16 at 20:02
  • @Kitson88 Thanks, I was beginning to doubt my sanity there for a second – RiggsFolly Oct 06 '16 at 20:03
  • Simple question, probably an embaracing answer! _What did you change 30 minutes ago???_ – RiggsFolly Oct 06 '16 at 20:04
  • @RiggsFolly Nah spot on and good interrogation skills. – Kitson88 Oct 06 '16 at 20:08
  • I tried @RiggsFolly 's suggestion and set the cookie above the html and then rechecked for the cookie and included the html file just after the body. It worked. However, I should tell you that the code hasn't moved from the of the index and was working for about a week so why was that the case??? – Ricky Payne Oct 06 '16 at 20:08
  • @RiggsFolly answer the question and I will star or whatever happens around here. – Ricky Payne Oct 06 '16 at 20:10
  • @RickyPayne I cannot do that now as the question has been closed with a Duplicate. Just read the dup and Upvote the answers you like or find helpful. Upvoting does not cost you any rep points – RiggsFolly Oct 06 '16 at 20:13
  • 1
    @RickyPayne It must of been there at some point. Maybe it was part of an include at the top of the page and you or someone else removed it and the code in the `` as in fact never worked. Or just maybe you defied the laws of PHP ;-) – Kitson88 Oct 06 '16 at 20:14
  • @Kitson88 Agree. Cookie has probably been there for days/weeks from when it did work and browser has been using that cookie ever since – RiggsFolly Oct 06 '16 at 20:16

0 Answers0