redirecting...

TarinC

New Member
Messages
698
Reaction score
0
Points
0
lets say that there is a site, but half the time you get the page cannot be displayed error.

i want to redirect to it but if it doesnt work, it redirects to another site. is this possible? and if it is can you tell me how?
 

Phil

Retired Staff
Messages
7,344
Reaction score
0
Points
36
TarinC said:
lets say that there is a site, but half the time you get the page cannot be displayed error.

i want to redirect to it but if it doesnt work, it redirects to another site. is this possible? and if it is can you tell me how?
You mean redirect by like a .tk?
 

TarinC

New Member
Messages
698
Reaction score
0
Points
0
site1 - The page i will create
site2 - the page i want to redirect to
site3 - in case 2 is not up.

i want to create a page that will redirect to 2. when the site isnt up it will redirect to a 3rd site instead of the 2nd site.

is there a code to do that?
 
Last edited:

Jim

New Member
Messages
170
Reaction score
0
Points
0
<?php
if(fopen("http://site2")){
echo "<script>window.location='http://site2';</script>";
} else {
echo "<script>window.location='http://site3';</script>";
}
?>
 
Last edited:

Bryon

I Fix Things
Messages
8,149
Reaction score
101
Points
48
Jim said:
<?php
if(fopen("http://site2")){
echo "<script>window.location='http://site2';</script>";
} else {
echo "<script>window.location='http://site3';</script>";
}
?>

Same thing I said. You should replace the javascript with just an HTTP header, and it will always work whether the vistor has JS disabled or not.
 

TarinC

New Member
Messages
698
Reaction score
0
Points
0
im sorry but im noob!

You should replace the javascript with just an HTTP header, and it will always work whether the vistor has JS disabled or not.

i dont get that.
 

TarinC

New Member
Messages
698
Reaction score
0
Points
0
Jim said:
<?php
if(fopen("http://site2")){
echo "<script>window.location='http://site2';</script>";
} else {
echo "<script>window.location='http://site3';</script>";
}
?>

i did that and i get a error:

Warning: fopen() expects at least 2 parameters, 1 given
in "the file" on line 7

line 7 of the script would be: if(fopen("http://site2")){
 
Last edited:

oab

New Member
Messages
918
Reaction score
0
Points
0
you have to put whether you want to read or write or somthing i think im to lazy to look it up..
 

Kay

New Member
Messages
804
Reaction score
0
Points
0
Here you go

Simply add this code to your php document:

Code:
<?php
header("Location: http://[color=red]www.blaa.com[/color]/");
?>

Change what is in red to your own website URL

You must add this on the first line of your document, else it will NOT work!
 

Bryon

I Fix Things
Messages
8,149
Reaction score
101
Points
48
That error is because fopen() expects at least the first 2 arguments to be passed to it. Check out the PHP manual on it: http://php.net/fopen

http://php.net/fopen said:
Description
resource fopen ( string filename, string mode [, bool use_include_path [, resource zcontext]] )

fopen() binds a named resource, specified by filename, to a stream. If filename is of the form "scheme://...", it is assumed to be a URL and PHP will search for a protocol handler (also known as a wrapper) for that scheme. If no wrappers for that protocol are registered, PHP will emit a notice to help you track potential problems in your script and then continue as though filename specifies a regular file.


I would recommend using:

PHP:
<?php
   $site1 = "http://www.yahoo.com";
   $site2 = "http://www.google.com";
   $handle = @fopen($site1, "r");
   if ($handle) {
	 fclose($handle);
	  header("Location: $site1"); // Redirecting to $site1
   } else {
	 fclose($handle);
	  header("Location: $site2"); // Redirecting to $site2
   }  
?>

Edit: Wait wait wait. I have to fix something
Edit 2: There ya go, I fixed it.
 
Last edited:

Bryon

I Fix Things
Messages
8,149
Reaction score
101
Points
48
Your welcome. Try to understand what it's doing. First it puts two URL's into two seperate variables, $site1, and $site2. Then it trys to open a connection to the first URL. If the connection fails, it will make $handle false, which would make the if statement not true for the first "part" of it, which would cause it to execute whatever is in the "else" part of it, thus redirecting the browser to $site2. If the connection is opened, $handle would be true, and contain the data put into it from fopen(), so the first "block" in the if statement would execute, and redirect the browser to $site1.

I hope you understand that...
 

TarinC

New Member
Messages
698
Reaction score
0
Points
0
ok i got it to work but lately it doesnt work. here is the error log:

[07-Nov-2005 22:50:51] PHP Warning: fclose(): supplied argument is not a valid stream resource in /backup/home/tpsx10h/public_html/tps/custom/php/server.php on line 9
[07-Nov-2005 22:52:55] PHP Warning: fopen(http://tps.servecounterstrike.com:27016/ip.w): failed to open stream: Connection timed out in /backup/home/tpsx10h/public_html/tps/custom/php/server.php on line 4

it used to work but now i got this...

inside the file:
Code:
<?php
$site1 = "[url="http://tps.servecounterstrike.com:27016/ip.w"]http://tps.servecounterstrike.com:27016/ip.w[/url]";
$site2 = "[url="http://tps.x10hosting.com/tps/custom/html/offline.html"]http://tps.x10hosting.com/tps/custom/html/offline.html[/url]";
$handle = @fopen($site1, "r");
if ($handle) {
fclose($handle);
header("Location: $site1"); // Redirecting to $site1
} else {
fclose($handle);
header("Location: $site2"); // Redirecting to $site2
}
?>

please help! thanks

for the script to start from the begining click here:
http://tps.x10hosting.com/tps/custom/html/loadingOLD.html
 
Last edited:

Bryon

I Fix Things
Messages
8,149
Reaction score
101
Points
48
Hmm I'm not sure why your getting those errors. Have you used it for a link before such as:
http://tps.servecounterstrike.com:27016/ip.w

?

I would think that it would work fine, but it might be the different port other then php, http, and ftp that PHP isn't allowing to open. Let me check quick what stream types are allowed in php.ini.

Ok well, this is what I found:
Registered PHP Streams: php, http, ftp, compress.zlib

So that might be the problem.

I might be wrong with all this though, if the port 27016 has an HTTP or an FTP server residing on it. :-\

Hmm. So I guess the question is, did the script before work fine?

If so, what did you change that caused it to stop working?
 

TarinC

New Member
Messages
698
Reaction score
0
Points
0
it worked fine on november 6 and earlier.

i changed the file directory and i came back to this forum remade the script:

<?php
$site1
= "http://www.yahoo.com";
$site2 = "http://www.google.com";
$handle = @fopen($site1, "r");
if (
$handle) {
fclose($handle);
header("Location: $site1"); // Redirecting to $site1
} else {
fclose($handle);
header("Location: $site2"); // Redirecting to $site2
}
?>
 
Last edited:

Bryon

I Fix Things
Messages
8,149
Reaction score
101
Points
48
PHP:
<?php
   $site1 = "http://www.yahoo.com";
   $site2 = "http://www.google.com";
   $handle = @fopen($site1, "r");
   if ($handle) {
      header("Location: $site1"); // Redirecting to $site1
      fclose($handle);
   } else {
      header("Location: $site2"); // Redirecting to $site2
   }  
?>

Try that...
 
Top