Page 1 of 1

Redirecting URL's using PHP

Posted: Fri May 16, 2008 7:59 am
by eduardosbs
Hello all,

I need some help whit this subject:

I need to redrect URLs as the server time, I was trying some thing like this:

<?php
$hour = (int)date("H");

if ($hour >= 9 && $hour < 10) {
echo "<meta http-equiv="Refresh" content="5; url=http://www.google.com">";

} elseif ($hour >= 10 && $hour < 12){
echo "<meta http-equiv="Refresh" content="5; url=http://www.google.com">";

} elseif ($hour >= 12 && $hour < 13){
echo "<meta http-equiv="Refresh" content="5; url=http://www.google.com">";


(the code continues until 24h)

But is not working... could some one help me ??

Re: Redirecting URL's using PHP

Posted: Fri May 16, 2008 8:13 am
by onion2k
You need to fix your quotes. This...

Code: Select all

 
echo "<meta http-equiv="Refresh" content="5; url=http://www.google.com">";
 
... will break PHP. PHP will think the string ends at equiv=. Either backslash the quotes in the string, or use single quotes rather than double quotes.

Re: Redirecting URL's using PHP

Posted: Fri May 16, 2008 9:32 pm
by dbemowsk
Your code below is confusing.

Code: Select all

 
if ($hour >= 9 && $hour < 10) {
echo "<meta http-equiv="Refresh" content="5; url=http://www.google.com">";
 
} elseif ($hour >= 10 && $hour < 12){
echo "<meta http-equiv="Refresh" content="5; url=http://www.google.com">";
 
} elseif ($hour >= 12 && $hour < 13){
echo "<meta http-equiv="Refresh" content="5; url=http://www.google.com">";
 
First off,

Code: Select all

if ($hour >= 9 && $hour < 10)
is the same as

Code: Select all

if ($hour == 9)

Same with

Code: Select all

elseif ($hour >= 12 && $hour < 13)
that is the same as

Code: Select all

if ($hour == 12)
Not to mention your meta refreshes are all the same. The refresh is for 5 seconds and all the URLs go to http://www.google.com.

What are you trying to achieve here?

Re: Redirecting URL's using PHP

Posted: Sat May 17, 2008 1:08 pm
by suresh_2220
You can use PHP header() fn inorder to redirect URL. for Eg :

Code: Select all

 
            <?php
                            
                      header("Location: http://suresh-mobileweb.blogspot.com");
                        
            ?>
 
 

Re: Redirecting URL's using PHP

Posted: Mon May 19, 2008 1:20 pm
by eduardosbs
Ok all,

for my last issue Iám ok ! now I am trying to do some thing like this...

<?php
$hour = date('H');
$min = date('i');
echo $hour;
echo $min;
/* 9:00 horas as 9:30 */

if ($hour >= 9 && $min < 30) {
echo "<meta http-equiv=\"Refresh\" content=\"5; url=http://www.URL1.com\">";

/* 9:30 horas as 10:00 */

} elseif ($hour >= 9 && $min > 30) {
echo "<meta http-equiv=\"Refresh\" content=\"5; url=http://www.URL2.com\">";

/* 10:00 horas as 10:30 */

} elseif ($hour >= 10 && $min < 30){
echo "<meta http-equiv=\"Refresh\" content=\"5; url=http://www.URL3.com\">";

/* 10:30 horas as 11:00 */

} elseif ($hour >= 10 && $min > 30){
echo "<meta http-equiv=\"Refresh\" content=\"5; url=http://www.URL4.com\">";

/* 11:00 horas as 11:30 */

} elseif ($hour >= 11 && $min < 30){
echo "<meta http-equiv=\"Refresh\" content=\"5; url=http://www.URL5.com\">";

/* 11:30 horas as 12:00 */

} elseif ($hour >= 11 && $min > 30){
echo "<meta http-equiv=\"Refresh\" content=\"5; url=http://www.URL6.com\">";


}

/* the code continues..... */


I need to redirect to a diferent url after 30 minutes... but the code above is not working... some one could help me ?

Re: Redirecting URL's using PHP

Posted: Mon May 19, 2008 2:04 pm
by puke7
I googled "meta refresh content".
The first several links had your answer.
That number in content= is the number of seconds until the refresh.

Re: Redirecting URL's using PHP

Posted: Mon May 19, 2008 2:41 pm
by eduardosbs
puke7 wrote:I googled "meta refresh content".
The first several links had your answer.
That number in content= is the number of seconds until the refresh.

tks for your reply but, thats noto the point ! i know that content is the number of seconds until refresh...

I have a webpage and i need a code that do some thing like this:

when you type http://www.mywebpage.com you will be redirected after 5 seconds to a diferent webpage , so i need a code that do this wich 30 minutes to a diferent url like i typed above !

Re: Redirecting URL's using PHP

Posted: Mon May 19, 2008 2:57 pm
by onion2k
I really wish people wouldn't put things like "the code above is not working". It's useless. Unless you tell us what's happening, or not happening, and what you expect it to do, there's nothing anyone can really do.

Taking a guess though.. only the first 2 URLs work, right?

Think about your conditions. You have:

Code: Select all

if ($hour >= 9 && $min < 30) {
 
} elseif ($hour >= 9 && $min > 30) {
 
etc
What happens if $hour is 100 and $min is 20? It matches the first condition. If $hour is 100 and $min is 40 it matches the second condition. None of the other conditions will ever be matched because the first two catch every possibility.

Personally I'd use a switch anyway..

Code: Select all

<?php
$hour = date('H');
$min = date('i');
switch ($hour) {
    case 9:  $url = ($min < 30) ? "www.url1.com" : "www.url2.com"; break;
    case 10: $url = ($min < 30) ? "www.url3.com" : "www.url4.com"; break;
    case 11: $url = ($min < 30) ? "www.url5.com" : "www.url6.com"; break;
    default:  $url = "www.thisdomainifsomethinggoeswrong.com"; break;
}
echo "<meta http-equiv=\"Refresh\" content=\"5; url=http://".$url."\">";
 

Re: Redirecting URL's using PHP

Posted: Mon May 19, 2008 3:42 pm
by eduardosbs
onion2k wrote:I really wish people wouldn't put things like "the code above is not working". It's useless. Unless you tell us what's happening, or not happening, and what you expect it to do, there's nothing anyone can really do.

Taking a guess though.. only the first 2 URLs work, right?

Think about your conditions. You have:

Code: Select all

if ($hour >= 9 && $min < 30) {
 
} elseif ($hour >= 9 && $min > 30) {
 
etc
What happens if $hour is 100 and $min is 20? It matches the first condition. If $hour is 100 and $min is 40 it matches the second condition. None of the other conditions will ever be matched because the first two catch every possibility.

Personally I'd use a switch anyway..

Code: Select all

<?php
$hour = date('H');
$min = date('i');
switch ($hour) {
    case 9:  $url = ($min < 30) ? "www.url1.com" : "www.url2.com"; break;
    case 10: $url = ($min < 30) ? "www.url3.com" : "www.url4.com"; break;
    case 11: $url = ($min < 30) ? "www.url5.com" : "www.url6.com"; break;
    default:  $url = "www.thisdomainifsomethinggoeswrong.com"; break;
}
echo "<meta http-equiv=\"Refresh\" content=\"5; url=http://".$url."\">";
 

Hi ,

I really apreciate your reply... but could you explain a little bit the code? sorry but i am a begginer... i got the idea but not at all....
this code is very very important to me if you explain the idea for me i will be apreciate a lot !!!!

regards

Re: Redirecting URL's using PHP

Posted: Mon May 19, 2008 3:45 pm
by onion2k
Switch statements: http://uk3.php.net/switch
Ternary if operator: http://uk3.php.net/ternary (scroll down a little way)
echo function: http://uk3.php.net/manual/en/function.echo.php