oyegigi
oyegigi

Reputation: 11

Redirect a page if timestamp in URL string is expired

I've pieced together some code by looking at this post and this post. But I didn't quite get it.

I want to include a timestamp in my url and check to see if 2 months has passed from the timestamp. If we are within the 2 months, display the page normally. If more than 2 months has passed, redirect the page.

Here's what I have so far but it's not working. Any suggestions on how I can get this working properly?

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

Upvotes: 0

Views: 775

Answers (2)

oyegigi
oyegigi

Reputation: 11

So based on these comments and others, I've modified the code to this and it's working:

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

Upvotes: 0

byteptr
byteptr

Reputation: 1385

The code to calculate the difference between the timestamps should work if you remove the "echo" from line #2; i.e. change:

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

to

$timestamp = $_GET['ts'];

You also may want test isset($_GET['ts']) and handle that condition explicitly since its possible to enter the page without the 'ts' variable being set.

Upvotes: 1

Related Questions