drouie2014 2014-08-07 13:49
浏览 11

在第4次访问之前向用户展示一些内容

I currently have working code that checks for a cookie and if found, it won't show a block of HTML code. If not found, it shows the code and then sets a cookie to expire in 24 hours.That's pretty simple. I wan't to elaborate on it though and now show that block of HTML code the first 3 visits in a 24-hour period. On the 4th visit in 24-hours, I don't want to show that code. I started writing javascript that would check for 3 different cookies and it got to be too much for me. I wonder if there is an easier way. Here's what I have today:

<script type="text/javascript">
function getCookie(name) {
var dc = document.cookie;
var prefix = name + "=";
var begin = dc.indexOf("; " + prefix);
if (begin == -1) {
    begin = dc.indexOf(prefix);
    if (begin != 0) return null;
}
else
{
    begin += 2;
    var end = document.cookie.indexOf(";", begin);
    if (end == -1) {
    end = dc.length;
    }
}
return unescape(dc.substring(begin + prefix.length, end));
} 
var thecookie='bd3b5cookie';
var myCookie = getCookie(thecookie) ;
if (myCookie == null) {
console.log("not found");
        //add html content here
}
else {    
console.log(" found");
//dont show html code
}
</script>

<script type = "text/javascript">
//set cookie
var now = new Date();
var time = now.getTime();
time += 3600 * 8000;
now.setTime(time);
  document.cookie = 'bd3b5cookie=ok;expires='+now.toGMTString()+';path=/';
//console.log(document.cookie);

</script>
  • 写回答

2条回答 默认 最新

  • dpjtn48868 2014-08-07 13:59
    关注

    You could add a "visits" column to the users table, and increment its value with each visit/login. Then, using a Cron job, you could execute an UPDATE users SET visits = 0 statement every 24 hours using your web server time.

    评论

报告相同问题?

悬赏问题

  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号