【问题标题】:How to make a html code only display once a day?如何使html代码每天只显示一次?
【发布时间】:2022-01-24 01:12:27
【问题描述】:

我想显示一个预览播放器,但我要添加一个刷新代码,它会在 5 分钟内刷新页面,因此他们只能在 5 分钟内看到 html 代码,所以我的问题是如何设置它来制作cookie,因此它每天只会显示一次我的 HTML 代码。如果他们刷新它会说“你今天使用了你的预览版,明天回来”之类的......这是我尝试过的 非常感谢任何帮助我可以使用 HTML 或 PHP,这对我来说无关紧要:)

    <head>
    <script src="//code.jquery.com/jquery-1.10.2.js"></script>
    <script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-cookie/1.4.1/jquery.cookie.min.js"></script>
    </head>
    <body>
    <div id="dialog" title="Test dialog">
    MY HTML CODE HERE
    </div>
    </body>
    </script>
    $(document).ready(function() {
    
       // Make sure dialog is initially hidden:
       $('#dialog').dialog({autoOpen: false});
    
        // Check for the "whenToShowDialog" cookie, if not found then show the dialog and save the cookie.
        // The cookie will expire and every 2 days and the dialog will show again.
    
        if ($.cookie('whenToShowDialog') == null) {
    
            // Create expiring cookie, 1 days from now:
            $.cookie('whenToShowDialog', 'yes', { expires: 1, path: '/' });
    
            // Show dialog
            $('#dialog').dialog("open");        
        }
    
    });
    </script>
PREVIEW USED FOR TODAY COME baCK TOMORROW

【问题讨论】:

  • 你可以试试setInterval,这是最简单的方法
  • 如果客户端 1. 手动删除 cookie,2. 以隐私模式打开浏览器,3. 创建没有 javascript 代码的页面副本或注入自己的 javascript,您的检查将失败。对于稍微了解网络技术的人,很容易避免您的检查。所有检查都应基于客户端 IP + cookie + 所有其他可能的跟踪信息在后端服务器上运行。

标签: javascript php html


【解决方案1】:

您可以设置一个在用户访问网站的当天结束时过期的 cookie。

document.cookie = `visited=true; expires=${dateObjectForEndOfDay}`;

然后,当客户端访问该页面时,您可以读取 cookie 并查看它是否已设置。过期的 cookie 不会显示,因此如果最后一个 cookie 已过期,就好像用户从未访问过该站点一样。 (以下代码摘自w3Schools

function getCookie(cname) {
  let name = cname + "=";
  let decodedCookie = decodeURIComponent(document.cookie);
  let ca = decodedCookie.split(';');
  for(let i = 0; i <ca.length; i++) {
    let c = ca[i];
    while (c.charAt(0) == ' ') {
      c = c.substring(1);
    }
    if (c.indexOf(name) == 0) {
      return c.substring(name.length, c.length);
    }
  }
  return "";
}

const siteWasVisited = getCookie('visited');

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多