Is there a way to check if a host is up?

Rob picture Rob · Jan 13, 2011 · Viewed 9.2k times · Source

I'm trying to do this in PHP. I need to check if a specified host is "up"

I thought of pinging the specified host (though I'm not sure how I would, since that would require root. --help here?)

I also though of using fsockopen() to try to connect on a specified port, but that would fail too, if the host wasn't listening for connections on that port.

Additionally, some hosts block ping requests, so how might I get around this? This part isn't a necessity, though, so don't worry about this too much. I realize this one might get tricky.

Answer

buley picture buley · Jan 13, 2011

I typically do a simple cURL for a public page and see if it returns a 200. If you get a 500, 404, or anything besides a 200 response you know something fishy is up.