Php 每天表演一次 一些文本
想要:Php 每天表演一次 一些文本,php,javascript,jquery,Php,Javascript,Jquery,想要: 每个访问者每天显示一次此块(无注册)(默认情况下隐藏,cookie检查,应该使用什么?) 如果块已在今天显示,则今天不再显示 如果块显示为yeasterday且一天已过,则再次显示(如何正确检查这一天已过?) 我该怎么做 具有此功能的站点示例: 这取决于您希望此规则的严格程度。 为了完全准确,您必须使用数据库来维护显示给用户的用户id和块时间。 但是,如果这不是一个非常严格的规则,您可以通过存储 浏览器cookie中的时间,并基于该时间显示/隐藏div。 Cookie可以在javasc
这取决于您希望此规则的严格程度。 为了完全准确,您必须使用数据库来维护显示给用户的用户id和块时间。 但是,如果这不是一个非常严格的规则,您可以通过存储 浏览器cookie中的时间,并基于该时间显示/隐藏div。
Cookie可以在javascript和PHP中读取,只要适合您,就可以显示/隐藏div。我创建了使用Cookie实现需求的示例代码: 这取决于和
$(文档).ready(函数(){
if($.cookie('showOnlyOne')){
//还不到一天
//藏起来
$('#shownOnlyOnceADay').hide();
}否则{
//cookie已过期,或者用户从未访问该站点
//创建cookie
$.cookie('showOnlyOne','showOnlyOne',{expires:1});
//并显示div
$('#shownOnlyOnceADay').show();
}
});
此文本每天只能显示一次!
通过更改系统日期对其进行了测试。其他can cookie,您可以使用localStorage存储用户上次访问页面的时间。您可以使用类似的代码,假设您总是从
div
开始,并将其styledisplay
属性设置为none
:
<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.0/jquery.min.js"></script>
<script type="text/javascript" src="https://github.com/carhartl/jquery-cookie/raw/master/jquery.cookie.js"></script>
<script type="text/javascript">
$(document).ready(function() {
if( $.cookie('showOnlyOne') ){
//it is still within the day
//hide the div
$('#shownOnlyOnceADay').hide();
} else {
//either cookie already expired, or user never visit the site
//create the cookie
$.cookie('showOnlyOne', 'showOnlyOne', { expires: 1 });
//and display the div
$('#shownOnlyOnceADay').show();
}
});
</script>
</head>
<body>
<div id="shownOnlyOnceADay">
This text should only be shown once a day!
</div>
</body>
</html>
Cookie,尽管如果他们禁用了Cookie,它会显示每次不知道使用Cookie检查“一天过去了”。或者如果他们清除了Cookie。基本上,并没有办法绝对保证一个div每天只向任何观众展示一次。不要关心残疾人cookies@alf,请避免引用w3schools->我不知道如何使用cookies@Brain那为什么不用呢?让我们调用cookie“last_ad_time”,当加载页面时,如果未找到该cookie值,或者如果找到该值,则查看该值是否早于24小时,并显示广告,同时设置cookie。如果该值早于24小时,则不执行任何操作。如何检查“该值是否早于24小时”?使用php
time()设置cookie
功能。在javascript中使用new Date().getTime()
,取它们之间的差值,以秒为单位,转换为小时,并检查是否大于24。
<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.0/jquery.min.js"></script>
<script type="text/javascript" src="https://github.com/carhartl/jquery-cookie/raw/master/jquery.cookie.js"></script>
<script type="text/javascript">
$(document).ready(function() {
if( $.cookie('showOnlyOne') ){
//it is still within the day
//hide the div
$('#shownOnlyOnceADay').hide();
} else {
//either cookie already expired, or user never visit the site
//create the cookie
$.cookie('showOnlyOne', 'showOnlyOne', { expires: 1 });
//and display the div
$('#shownOnlyOnceADay').show();
}
});
</script>
</head>
<body>
<div id="shownOnlyOnceADay">
This text should only be shown once a day!
</div>
</body>
</html>
if(localStorage.last){
if( (localStorage.last - Date.now() ) / (1000*60*60*24) >= 1){ //Date.now() is in milliseconds, so convert it all to days, and if it's more than 1 day, show the div
document.getElementById('div').style.display = 'block'; //Show the div
localStorage.last = Date.now(); //Reset your timer
}
}
else {
localStorage.last = Date.now();
document.getElementById('div').style.display = 'block'; //Show the div because you haven't ever shown it before.
}