Click to See Complete Forum and Search --> : Connectivity Test


yoshi273
02-07-2002, 07:20 PM
I wanted to write a PHP script that checks to see if a webserver is up. If it isnt, it will redirect to another server that IS up. Here is what I was thinking so far...

<?php
$sp = fopen($sitename, "r");
if($sp)
{echo "$sitename is active";}
else
{echo "$sitename is not active";}
?>

Ok, so if I use yahoo.com as the sitename, it works fine. I tried another server that I currently know is down, and it didnt tell me it wasnt active...

Is there a better way to do this? Is there a way to make this thing set a timeout to 2 seconds? Basically, when im done with this, i want to have something liek this...

if(site does not work)
header("redirect to backup site");

if(site does work)
header("redirect to normal site");

It would also be cool if I had a way to ping the servers. I could test connectivity to a user across 4 or 5 sites and send them to the best one. Got any ideas for this? Without using system commands?

Barracuda
02-07-2002, 10:59 PM
This code doesn't actually provide for a good test because it tests your web servers connectivity to the web site you query, not the clients connection. Your web server might be able to get through, but your client might not.

If you still want to do this though, I have a suggestion that will make this faster and conserve bandwidth. What you would want to do is open a socket connection on port 80 of the site you are testing, which will connect you to their web server. Then write the following to the socket:

HEAD / HTTP/1.0\n\n

After doing that, read from the socket and you'll get the headers back from the server. This is a lot faster than retrieving the content of the entire web page at the root level, which is what your current code does. Not sure about how to do this with PHP, so I can't help you out there http://www.sharkyforums.com/ubb/smile.gif

yoshi273
02-08-2002, 12:43 AM
ok, thanks for the advise. I think something called fsockopen("site", 80) will open a socket to port 80 and attempt to grab data from it. Ill give it a try...

biosx
02-08-2002, 11:24 AM
Yoshi,

If you are more worried about the task and not the language you can use a Perl script. Perl can do that easily.



------------------
## root is the greed of all evil ##