Time Base Redirect

Is it possible to to time based website redirect?

Suppose I want show user can visit my page from 06:00AM to 12:00PM then after it any visit the page it will direct redirect to (timeout.html)

I search on Google in - (JS | .Htaccess) but there I not found.

Have you idea ?

I think I’d be more inclined to use PHP for this.

Is php help for this?
I use htacess . Will it work?

RewriteEngine On
RewriteCond %{TIME_HOUR}%{TIME_HOUR} >2109
RewriteRule ^$ /retail/dashboard.php [R=301,L]

Try it and see!

1 Like

I use PHP, But why this is not working?

<?php
$time = date('G'); //24 hour without leading zeroes

if($time >= 9 && $time <= 9)         
$loc = 'good.php';

else if($time >= 21 && $time <= 3)    
$loc = 'bad.php';

else if($time >= 3 && $time <= 5)    $loc = 'sleep.php';

if(isset($loc)) {
    header("Location: https://abcd.com/{$loc}");
    exit;
}
?>

Not working…Time waste@@

Some of your conditions make no sense and the syntax is wrong.

Always use error reporting when testing php to find errors quickly. Though I expect this advice to go the same way as the multiple times you have been advised to use the validator to identify errors in your html syntax, in which case you are refusing to be helped and thus cannot be helped.

Did you means I Validate this php on “W3VALIDATOR”

<?php
$time = date('G'); //24 hour without leading zeroes
if($time >= 9 && $time <= 9)
$loc = 'good.php';
else if($time >= 21 && $time <= 3)
$loc = 'bad.php';
else if($time >= 3 && $time <= 5) $loc = 'sleep.php';
if(isset($loc)) {
header("Location: https://abcd.com/{$loc}");
exit;
}
?>

After again validation it not working and not redirect…

Why?

if($time >= 9 && $time <= 9)

This will only be true when $time is equal to 9. I doubt this is what you mean.

1 Like

That validator of for html. Use if on your html.
For php enable error reporting. It can be configured to display errors on screen in a development environment, or can log errors to a file on a live server.
Any errors and warnings are identified when the script is run. This is essential in debugging and development, you are stumbling around in the dark without it.

…And when can a value be greater than 20 and less than 4?

1 Like

9=9 it was by mistake…

I want create …Like;
From 09AM to 09PM=good.php
From 09PM to 03AM=bad.php
From 03AM to 06AM=sleep.php

Now you can understand what I want saying…

Thank you for focus.
But when I correct it nor working…

What does your corrected code look like?

<?php
$time = date('G'); //24 hour without leading zeroes
if($time >= 9 && $time <= 21)
$loc = 'good.php';
else if($time >= 21 && $time <= 3)
$loc = 'bad.php';
else if($time >= 3 && $time <= 8) $loc = 'sleep.php';
if(isset($loc)) {
header("Location: https://website.com/{$loc}");
exit;
}
?>

I replaced the 9
And then put it in page but now it not working …

When you post code on the forums, you need to format it so it will display correctly. You can highlight your code, then use the </> button in the editor window, or you can place three backticks ``` (top left key on US/UK keyboards) on a line above your code, and three on a line below your code. I find this approach easier, but unfortunately some European and other keyboards don’t have that character.

1 Like

Actually I am new on sitepoint I not have knowledge very how to use and what is the purpose of fa-fa icons inside textarea when I creating a post.

Whatever Could you help…On this topic…

Basic syntax - you forgot the curly braces around the if/else statements.

1 Like

Not understand…
Suggestions is better then practice

There was a suggestion that you enable PHP error reporting.
Did you do that?