Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set session/cookie for 24hs to show something only once per day to users

I've been searching for the last hour how to do this... I think it's pretty easy, but couldn't make it work.

I want to show something (AdSense) only once per day.

I don't know what would be the best way, if using cookies or PHP Sessions. In any case, can you help me and tell me how could I do this?

Thanks in advance!

Santiago

Edit: I think this way I can create the Session, but I don't know how to "recreate" it each 24 hours and how to check for that session in order to show what I want to show once a day.

if (!isset($_SESSION['adSense']) 
    $_SESSION['adSense'] = time();

if (time() - $_SESSION['adSense'] <= 60*60*24 ) {
   return true;
} else {
   return false;
}
like image 262
Santiago Avatar asked Nov 30 '22 02:11

Santiago


1 Answers

Sessions will expire when the user leaves the site, using cookies is what you want:

<? if (!isset($_COOKIE['showstuff'])): ?>

    <!-- replace this whatever you want to show -->

    <?
    setcookie('showstuff', true,  time()+86400); // 1 day
    ?>

<? endif; ?>
like image 139
IMB Avatar answered Dec 05 '22 00:12

IMB