Redirecting URL's using PHP

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
eduardosbs
Forum Newbie
Posts: 4
Joined: Fri May 16, 2008 7:45 am

Redirecting URL's using PHP

Post 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 ??
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: Redirecting URL's using PHP

Post 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.
dbemowsk
Forum Commoner
Posts: 82
Joined: Wed May 14, 2008 10:30 pm

Re: Redirecting URL's using PHP

Post 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?
Last edited by dbemowsk on Sat May 17, 2008 10:55 pm, edited 1 time in total.
suresh_2220
Forum Newbie
Posts: 23
Joined: Mon May 12, 2008 3:13 pm

Re: Redirecting URL's using PHP

Post 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");
                        
            ?>
 
 
eduardosbs
Forum Newbie
Posts: 4
Joined: Fri May 16, 2008 7:45 am

Re: Redirecting URL's using PHP

Post 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 ?
User avatar
puke7
Forum Newbie
Posts: 12
Joined: Sat May 10, 2008 11:49 am

Re: Redirecting URL's using PHP

Post 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.
eduardosbs
Forum Newbie
Posts: 4
Joined: Fri May 16, 2008 7:45 am

Re: Redirecting URL's using PHP

Post 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 !
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: Redirecting URL's using PHP

Post 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."\">";
 
eduardosbs
Forum Newbie
Posts: 4
Joined: Fri May 16, 2008 7:45 am

Re: Redirecting URL's using PHP

Post 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
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: Redirecting URL's using PHP

Post 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
Post Reply