Problem with creating cookie and showing/hiding div based on it...

Discussion in 'PHP' started by Error404, Apr 24, 2009.

  1. #1
    Hi there,

    I am trying to set a cookie on html page with php code, which if cookie is not yet set shows content of a specific Div on that page (in this case it's div called Autoload). But after it's set (which is on next page refresh or reload) I want it to do nothing.

    I came up with following code, but it wont work... It always displays Div content even if the cookie has been set (or probably because I haven't set it properly).

    So what I need is:
    User goes to page, Div content is shown, then on next refresh/reload Div is not shown anymore.

    What am I doing wrong?

    Here is the code I've put in the BODY of html page:


    <?php
     if(isset($_COOKIE['imekukija']))
     {
     }
     else
     { echo
     ?>
     <div id="autoload">
     <script src="lightbox/prototype.js" type="text/javascript"></script>
     <script src="lightbox/scriptaculous.js?load=effects" type="text/javascript"></script>
     <script src="lightbox/lightbox.js" type="text/javascript"></script>
     <a href="http://www.webmaster-talk.com/images/index_open.jpg" rel="lightbox" id="img"></a>
     </div>
     <?php
      }
     ?>
     
     
     <?php
     setcookie("imekukija", "nekiValue", time()+3600);
     ?>
    Code (markup):
     
    Error404, Apr 24, 2009 IP
  2. SHOwnsYou

    SHOwnsYou Peon

    Messages:
    209
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I'm not positive, but I think you need to use ob_start(); as the very first line of output (before <html>) and ob_flush(); as the final line of output if you aren't sending cookies as the first action.
     
    SHOwnsYou, Apr 24, 2009 IP