php 将会话/cookie 设置为 24 小时以每天只向用户显示一次内容

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12099170/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 02:48:57  来源:igfitidea点击:

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

phpsessioncookies

提问by Santiago

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.

我想每天只展示一次 (AdSense)。

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?

如果使用 cookie 或 PHP Sessions,我不知道最好的方法是什么。无论如何,你能帮助我并告诉我我该怎么做吗?

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.

编辑:我认为这样我可以创建会话,但我不知道如何每 24 小时“重新创建”它以及如何检查该会话以显示我想每天显示一次的内容。

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

if (time() - $_SESSION['adSense'] <= 60*60*24 ) {
   return true;
} else {
   return false;
}

回答by IMB

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

当用户离开站点时,会话将过期,使用 cookie 是您想要的:

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

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

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

<? endif; ?>

回答by Muhammad Shahzad

You can do this way.

你可以这样做。

1.

1.

setcookie('showPopup','yes',time() + 24 * 3600); // 24 hours

2.

2.

setcookie('showPopup','yes',strtotime( '+1 days' )); // 24 hours

3.

3.

setcookie('showPopup','yes',time() + 86400); // 24 hours

Check Cookie

检查饼干

if(isset($_COOKIE['showPopup']))
{
     echo 'Not show pop';
}
else{           
    echo 'show popup and set cookie';
    setcookie('showPopup','yes', time() + 86400);
}

回答by Majid Laissi

How about doing it the other way:

换一种方式如何:

  • You check for the existence of the cookie.
  • if it's there, do nothing
  • if it's not there, do what you want to do once a day, and create a cookie that expires 24 hours later.
  • 您检查 cookie 是否存在。
  • 如果它在那里,什么都不做
  • 如果它不在那里,每天做一次你想做的事,然后创建一个 24 小时后过期的 cookie。