Log in

View Full Version : Redirect Based On Time



redtrain
12-21-2006, 07:45 PM
Hi....I'm really desperate for a script that enables me to do this, so if anyone can help that would be great.

I am writing a signup page that I only want users to access from 6.00pm-9.00pm on any day. The rest of the time I want users to be forwarded to a different page, telling them that they must signup between 6.00pm-9.00pm.

Ideally, I would like to put the signup page on the same page as the php code which redirects the user, so that they can't access it out of hours.

If you can help that would be great. If you can only think of a variation on this, thats great too!

Thanks!

PS: I did search, but nothing proved helpful/didn't work.

thetestingsite
12-21-2006, 08:01 PM
You could try the following as a template. (Please note that this will go off of the server time, not the client's machine)



<html>
<head>
<title>Sign-up</title>
</head>
<body>

<?php
$curTime = date('G',time()); //gets current hour [24 hour]

if ($curTime <= "18" || $curTime >= "21") {
/* if the time is less than 6pm (1800) or greater than 9pm (2100) display error */
?>

You must signup in between 6:00pm and 9:00am (our server time).

<?php

}

else {
//else display signup form

?>

<form action="register.php">
<!--Your form data here-->
</form>

<?php
}

?>

</body>
</html>


I haven't tested this out, but it should work with little or no modification. Let me know if you need any more help.

thetestingsite
12-21-2006, 08:19 PM
OK...so i tested it out on my home server and was able to play around with the date, and I found two mistakes in my previous code.

replace the following:



$curTime = date('G',time()); //gets current hour [24 hour]

if ($curTime <= "18" || $curTime >= "21") {


with:



$curTime = date('Gi',time()); //gets current hour [24 hour]

if ($curTime <= "1800" || $curTime >= "2100") {


and that makes it work. Hope this helps.

redtrain
12-21-2006, 08:36 PM
Thank you so much!

I don't suppose you know how I can find my server time?:) It's still coming up with the message, rather than the form, but I think its because I've got the wrong time...(for the server...)

redtrain
12-21-2006, 09:00 PM
Ok....I think there's something wrong with the script....I've opened the time from 0000 to 1259 and it still brings up the message.

thetestingsite
12-22-2006, 02:13 AM
if you want to find out the server's time, make a PHP script (let's call it curTime.php). In the script, place the following code:



<?php

echo 'Current time (12 hour): '.date("h:i:s", time()).'<BR>';

echo 'Current time (24 hour): '.date("H:i:s", time());
?>


Both of those will tell you the current server time in hours:minutes:seconds in 12/24 hour formats. If this doesn't help, let me know and I'll see what else I could do.

BLiZZaRD
12-22-2006, 05:53 AM
For a little bit simpler you could just use the time based header redirect:



<?php
if(date("H")<8)
header("Location: http://www.yoursite.com/path/to/page.php")
?>


You just place this on the VERY top of the page (before the <html> or any other space, character etc. on the non-form page. and fill in the Location URI to the form/sign up page.

Note the "H" is php for hour, so this code exactly will redirect between 12 midnight and 8 a.m. server time.

Or for more advanced options see this thread (http://www.dynamicdrive.com/forums/showthread.php?p=24032#post24032) with particular attention to reply #5 and #6