一旦时间结束,您可以将 cookie 设置为“已过期”...当页面加载时,如果 cookie 为“已过期”,那么您可以显示“已过期”警报。您还可以使用 cookie 来跟踪累积的浏览时间。
编辑 - 添加了一些细节...但我认为您必须再考虑一下。
基本上,您想在用户使用页面时使用 JS 写入 cookie,而您希望在页面加载时使用 PHP 读取 cookie。您可以使用 cookie 来仅跟踪时间是否已到、总累积时间或两者兼而有之。我想你想每分钟左右更新一次 cookie?
它会看起来像这样SOMETHING - 此代码仅显示如何使用 cookie 跟踪时间是否已过期,而不是累积时间。
<?php
$total_mints=($live_match['match_name']) * (60);
// check for cookie and only proceed if it is not expired
// can also use cookie to keep track of total accumulated number
// of minutes between session
if ($_COOKIE["yourMints"] != "expired")
{
?>
<script language="text/javascript">
display_c(<?php echo $total_mints; ?>,'ct');
</script>
<script type="text/javascript">
function display_c(start,div)
{
window.start = parseFloat(start);
var end = 0 // change this to stop the counter at a higher value
var refresh=1000; // Refresh rate in milli seconds
if(window.start >= end )
{
mytime=setTimeout("display_ct('"+div+"')",refresh)
} else
{
alert("Time Over ");
// set cookie to expired
document.cookie = "yourMints=expired";
}
}
</script>
<?php
} else // What follows is what happens if cookies IS expired
{
?>
<script type="text/javascript">
alert("Time Over ");
</script>
<?php
}
?>
这是一个很好的 JS cookie 教程:
http://www.quirksmode.org/js/cookies.html
这里是使用 $_COOKIE 通过 PHP 读取 cookie
http://php.net/manual/en/reserved.variables.cookies.php
编辑:在看到 PlagueEditor 的示例后在 JQuery 示例中添加。
不错的脚本 PlagueEditor。以为我会尝试使用 JQuery 来做同样的事情。
JQuery 有一个simple little cookie plugin...只有 40 行左右的代码。
这是一个带有 cookie 存储计时器的页面和 10 秒的超时时间,可能会重置:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Time Spent on Page</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script type="text/javascript" src="PATH-TO-YOUR-JQ-DIRECTORY/jquery-1.4.2.min.js"></script>
<script type="text/javascript" src="PATH-TO-YOUR-JQ-DIRECTORY/cookie.js"></script>
<script type="text/javascript">
<!--
$.myTimer =
{
timeLimit: 10,
displayTime: function ()
{
if ($.myTimer.time < $.myTimer.timeLimit)
{
$("#timeHere").html($.myTimer.time);
$.cookie('yourMints', $.myTimer.time, { expires: 7});
++$.myTimer.time;
$.myTimer.toggle = setTimeout("$.myTimer.displayTime()",1000);
} else
{
$("#page").html('<h1>Time expired</h1>');
}
}
}
// When the page is ready ==================================================
$(document).ready(function()
{
// Read time spent on page cookie. Set it, if it doesn't exist.
if (!$.cookie('yourMints'))
{
$.cookie('yourMints', '0', { expires: 7});
}
$.myTimer.time = $.cookie('yourMints');
// Start timeer
$.myTimer.displayTime();
// Reset the timer
$("#reset").click( function()
{
$.cookie('yourMints', '0');
window.location.reload();
});
});
// -->
</script>
</head>
<body>
<div id="page">
<h2>Your total time here: <span id="timeHere"></span></h2>
You can only look at this page for 10 seconds.
</div>
<input id="reset" type="button" value="Reset Timer" />
</body>
</html>