【问题标题】:Redirect a page if timestamp in URL string is expired如果 URL 字符串中的时间戳已过期,则重定向页面
【发布时间】:2015-09-26 16:19:01
【问题描述】:

我通过查看this postthis post 拼凑了一些代码。但我不太明白。

我想在我的 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");
}

【问题讨论】:

  • 所以...你在找什么? :D
  • 你的问题是什么??
  • 对。对不起。我的问题是:如何修改代码以使其正常工作?

标签: php string url redirect timestamp


【解决方案1】:

如果您从第 2 行中删除“echo”,则计算时间戳之间差异的代码应该可以工作;即改变:

$timestamp = echo $_GET['ts'];

$timestamp = $_GET['ts'];

您可能还需要测试 isset($_GET['ts']) 并明确处理该条件,因为它可以在没有设置 'ts' 变量的情况下进入页面。

【讨论】:

  • add_action('wp', 'registration_discount'); function registration_discount(){ if($_SERVER["REQUEST_URI"] == '/registration-discount') { if(isset($_GET['ts'])){ if ((time() - $timestamp) >= 5256000){ { 回声'有效'; } else { echo 'funkytownusa.com">' }
【解决方案2】:

因此,基于这些 cmets 和其他人,我已将代码修改为此并且它正在工作:

// 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");
        }
    }
  }
}

【讨论】:

    猜你喜欢
    • 2012-10-05
    • 1970-01-01
    • 2014-02-11
    • 1970-01-01
    • 2018-07-13
    • 2016-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多