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!
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?!?
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!
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...
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"> ?
<?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...