IF/THEN statement to pick which site to navigate to?

MikeHGA79

New Member
Messages
24
Reaction score
0
Points
0
Ok...I'll do my best to explain. Essentially I have two identical websites (mirrors I guess) that are with two different hosts. Let's call them Site A & Site B. I'd like to have a 3rd site to serve as a portal. We'll call that "Portal". All users will access Portal....at that time I'd like to have some type of script that would look at Site A, if Site A is up, then proceed to Site A. If Site A is down, go to Site B. How is this possible? Currently I just have a iFrame sitting in the Portal and it's pointing to whatever site is up and/or running faster. Of course I am manually updating this throughout the week. I'm using some free website monitoring services that I thought I could use in this whole formula I'm just not sure how to implement it.

Any ideas? And oh yeah...the goal is to do all this for zero $$.

Thanks!
 

Livewire

Abuse Compliance Officer
Staff member
Messages
18,169
Reaction score
216
Points
63
Ok, keep in mind this is ABSOLUTELY POSITIVELY UNTESTED AND I DO NOT TAKE ANY RESPONSIBILITY FOR WHAT THIS CODE MAY OR MAY NOT DO PROPERLY!

Thank you.


What I would try is using something similar to this:

Code:
$site_up=fopen("www.site.com/index.php","r");
if ($site_up != false) {
fclose($site_up); //close this out so its not tying up resources.
header("Location: www.site.com");
}
else {
header("Location: www.site2.com");
}


Basically, fopen will try to open that website address. If it does open, great - the site must be up, so close the newly opened file handler and redirect the browser to that website.

If it doesn't open (fopen returns false on error), then send the browser to the other site.


It's kinda ugly though; I've never used fopen, so I've got no clue how it handles opening a page like that - in theory it should work, but I've got no testing on that, and I don't have any knowledge on how to change the headers being sent to the browser so it changes the location - I think I have it right, but I wouldn't be surprised to find it wrong >_<


In any case, the basic idea should work just fine:

1) Try to open a file on the main site.
2) If we can open it, send the user to the main site (if we can access the file, then the site must be up). If we can't, send them to the backup.


That could even be expanded so it'd go down a list of multiple backups - each time it can't open the site, have it try another one.


Hope that helps - please don't try it publically first, try it somewhere private so if it does implode, it doesn't do it to your websites >_<

Might not hurt to wait for someone else to see this and see what they say too.
 

Hazirak

New Member
Messages
197
Reaction score
0
Points
0
Tested the script. It works with a few minor (and I truly mean minor) modifications.

Code:
<?php
$site_up=@fopen("http://www.site1.com/index.php","r");
if ($site_up != false) {
	fclose($site_up); //close this out so its not tying up resources.
	header("Location: http://www.site1.com/index.php");
}
else {
	header("Location: http://www.site2.com/index.php");
}
?>

First of all, you NEED that 'http://' part infront of the URLs, otherwise it thinks you're looking for a file or directory at the current server named "www.site.com". Obviously that's not the effect we want.

Second of all, while the script works if SiteA is up, PHP will throw an error and cancel execution of the script instead of redirecting to SiteB if SiteA is not up. Thus, we need to include an @ symbol in front of the call to fopen() to suppress the warning and allow the script to continue as intended.
 
Top