我通过查看这篇文章和这篇文章拼凑了一些代码。但我不太明白。
我想在我的url中包含一个时间戳,并检查是否2个月已经从时间戳过去了。如果我们在2个月内,正常显示页面。如果超过2个月过去了,重定向页面。
这是我到目前为止所做的,但它不起作用。有什么建议,我如何才能得到这个工作正常吗?
//test timestamp... this will come from the url as so: www.mywebsite.com?ts=1340037073
$timestamp = echo $_GET['ts'];
//check if it has expired (in seconds, 5256000 sec = 2 months).
if ((time() - $timestamp) < 5256000)
{
echo 'valid';
}
else
{
Header("Location: http://www.google.com");
}
计算时间戳之间的差异的代码应该工作,如果你从#2行删除"echo";即改变:
$timestamp = echo $_GET['ts'];
$timestamp = $_GET['ts'];
您也可能需要测试isset($_GET['ts'])并显式处理该条件,因为有可能在不设置'ts'变量的情况下进入页面。
所以基于这些评论和其他,我已经修改了代码,它的工作:
// Registration Discount Page
//check if it has expired (in seconds, 5256000 sec = 2 months).
add_action( 'wp', 'registration_discount');
function registration_discount(){
if(strpos($_SERVER["REQUEST_URI"], '/registration-discount') > -1 ){
if(empty($_GET['ts'])){
header("location: http://www.funkytownusa.com");
}
elseif(isset($_GET['ts'])){
if ((time() - $_GET['ts']) >= (DAY_IN_SECONDS * 60))
{
header("location: http://www.funkytownusa.com");
}
}
}
}