Sorry I'm still very new at this and there is probably an easier way to write this code. I want to have a contest page automatically redirect to another after a certain date. I have the code to turn on and off content after a certain date and I have code for a simple redirect, but I can’t figure out how to combine the two into something that doesn’t look ugly and complex. Any help would be greatly appreciated. This is what I have so far:

<?php $timeoff = '2012-02-10 00:00:00';
$timenow = date('Y-m-d H:i:s');
$showtxt = ($timenow >= $timeoff) ? false : true;
if ($timeoff = true);
header( "HTTP/1.1 301 Moved Permanently" );
header( "Status: 301 Moved Permanently" );
header( "Location: http://www.redirectsite.com" );
exit(0); 
} else {
}
?>

Your code isn't comparing correctly. You want to see whether text needs to be shown using the 'if' condition, but you are comparing a date? You are also using the assign operator '=' instead of the comparing operator '==':

Line 4-10 should be:

if ($showtxt == false) {
...
...
...
...
} else {
  echo "Page content";
}

I'm not sure whether you can compare dates like that, but I guess you can find that out by yourself. You also might consider indenting & commenting your code!

~G

Thank you 'Graphix' for your help, however you may have to hold my hand a bit with this as I'm still learning. So with your corrections my code should look like this then?

<?php $timeoff = '2012-02-10 00:00:00';
	$timenow = date('Y-m-d H:i:s');
		$showtxt = ($timenow >= $timeoff) ? false : true;
			if ($showtxt == false) {
				header( "HTTP/1.1 301 Moved Permanently" );
				header( "Status: 301 Moved Permanently" );
				header( "Location: http://www.redirectsite.com" );
			exit(0); 
		} else { echo "http://www.contestpage.com"; }
?>

It seems like a simple task, to make a page non-visable after a certain date, do you know if there an easier/cleaner way of doing this?

Thanks again. I really appreciate your help.

~MB

Member Avatar for Zagga

Hi Marty1963,

I would go with the following code.

<?php
$timeoff = '2012-02-10 00:00:00';
$timenow = date('Y-m-d H:i:s');
if ($timenow >= $timeoff) {
	header( "HTTP/1.1 301 Moved Permanently" );
	header( "Status: 301 Moved Permanently" );
	header("Location: http://www.redirectsite.com");
	exit(); 
} else {
	echo "http://www.contestpage.com";
}
?>

The indentation Graphix mentioned is to make your code a bit easier to understand, so you just need to indent 'related' blocks of code.

On a side note, the date function will return the date at the location of your server, not necessarily the location of your users. You may want to bear this in mind.

When I meant 'indenting & spacing', I meant it a bit differently than you did it (see the code). Also you can't compare dates using the strings, you first need to convert them to UNIX timestamps (see http://php.net/manual/en/function.strtotime.php for more explaination).

<?php 
/* The date limit when the page needs to be redirected: */
$timelimit = '2012-02-10 00:00:00';

/* The current date: */
$timenow = date('Y-m-d H:i:s');

/* Comparing the two dates: */
if ( strtotime($timenow) > strtotime($timelimit) ) {

	/* Redirecting user to another page: */
	header( "HTTP/1.1 301 Moved Permanently" );
	header( "Status: 301 Moved Permanently" );
	header( "Location: http://www.redirectsite.com" );
	exit(0); 

} else { 

	/* Showing page content: */
	echo "http://www.contestpage.com"; 
}

?>

As for the redirecting, I think it is either this or a JavaScript redirect (which is client side). Try googling it.

~G

Thank you 'Graphix' and 'Zagga' for your help! That's just what I was looking for.
~Cheers!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.