Server time + cookies help needed.

Discussion in 'PHP' started by mightyb, Apr 4, 2007.

  1. #1
    I want to display a different image depending on the server time only once a day to every unique user. If the user has cookies disabled, i want no image to be shows. Its just they will find it quite annoying if the image will not get stopped by the cookies.

    So i have the server time code here:

    
    <?php
    $h = date('G');
    
    if ($h = 1) $img = '1.jpg';
    else if ($h = 2) $img = '2.jpg';
    else if ($h = 3) $img = '3.jpg';
    else if ($h = 4) $img = '4.jpg';
    //and so on
    ?>
    
    Code (markup):
    Then i just use this: <img src="<?php echo $img; ?>">

    Does it look like it will work? Ok, so that is out of the way.

    What about cookies? How do i make sure <img src="<?php echo $img; ?>"> is only displayed once a day and only to those with cookies enabled?

    Thanks a lot guys!
     
    mightyb, Apr 4, 2007 IP
  2. picouli

    picouli Peon

    Messages:
    760
    Likes Received:
    89
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Well, first of all if you have 24 images, one per hour, you could more simply do:
    <img src="<?php echo date('G'); ?>.jpg">
    HTML:
    For the other part of your question, do you want your users to see the image just *once*? Since you have 24 images (one per hour) this will mean that in a certain hour they will see the image once and then they won't see it anymore for the rest of the hour - is this what you want?!?
     
    picouli, Apr 4, 2007 IP
    mightyb likes this.
  3. mightyb

    mightyb Banned

    Messages:
    6,566
    Likes Received:
    405
    Best Answers:
    0
    Trophy Points:
    0
    #3
    lol good point.


    No, i want them to see only one image a day. So if they visit the site at 9:00, 9.gif will be the only image they will see once for the rest of the day.

    Thanks!
     
    mightyb, Apr 4, 2007 IP
  4. picouli

    picouli Peon

    Messages:
    760
    Likes Received:
    89
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Ah, ok - then what you can do is:
    1) set a cookie that will "die" at midnight with the image name (that is actually the hour when the visitor came to the site) - add this at the top of your script:
    <?php
      if (! $_COOKIE['image']) {
        $midnight_time = time() + ((24 - date('G')) * 3600); # more or less, this is the timestamp of midnight
        setcookie("image", date('G'), $midnight_time);
      }
    ?>
    PHP:
    2) then, where you show the image:
    <img src="<?= ($_COOKIE['image'] ? $_COOKIE['image'] : date('G')) ?>.jpg">
    
    HTML:
    I hope it will work... ;)
     
    picouli, Apr 4, 2007 IP
  5. mightyb

    mightyb Banned

    Messages:
    6,566
    Likes Received:
    405
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Looks good, green rep for you :D Il give it a try now.
     
    mightyb, Apr 4, 2007 IP
  6. mightyb

    mightyb Banned

    Messages:
    6,566
    Likes Received:
    405
    Best Answers:
    0
    Trophy Points:
    0
    #6
    For some reason the first code gives me Parse error: syntax error, unexpected '<'

    I have also made a few changes to the server date code... How would i go about displaying <?php echo $img; ?> rather than <img src="<?php echo date('G'); ?>.jpg"> ?
     
    mightyb, Apr 4, 2007 IP
  7. picouli

    picouli Peon

    Messages:
    760
    Likes Received:
    89
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Sorry, which code gives you error? You shouldn't have problems with any of the code above...
     
    picouli, Apr 4, 2007 IP
  8. mightyb

    mightyb Banned

    Messages:
    6,566
    Likes Received:
    405
    Best Answers:
    0
    Trophy Points:
    0
    #8
    
    <?php
      if (! $_COOKIE['image']) {
        $midnight_time = time() + ((24 - date('G')) * 3600); # more or less, this is the timestamp of midnight
        setcookie("image", date('G'), $midnight_time);
      }
    ?>
    
    PHP:
    This one. Or could this be because of smarty? {PHP} tags gave me more errors, {literal} however worked fine but i could not find any cookies in my browsers. Hmmmm...
     
    mightyb, Apr 4, 2007 IP