PHP Server up or Down Script.

Powermaster

Site Supporter
I am tring to compose this script to tell when a server is up or down.


PHP:
<?
$fp = fsockopen('apple.com', '80', $errno, $errstr, 2);
	if(!$fp) {
    	echo '<font color="#FF0000" face="Arial, Helvetica, sans-serif">Down</font>';
} 	else {
		socket_set_timeout($fp, 0,1);
		echo '<font color="#00FF00" face="Arial, Helvetica, sans-serif">UP</font>';
}

?>

Works just fine but, when pluging in a port in the is closed I get a nasty output.

See http://www.powermaster.cc/test.php

Thanks for your suggestions!
 
Cute program! I can use something like that...

Anyway, simple soluition.

PHP is going through the script and executing everything line by line, when it has an error, it responds to it immediately and never reaches your 'pretty' conditional.

Use the error supression operator (@) before the fsockopen command, like so:
PHP:
$fp = @fsockopen('www.apple.com', '80', $errno, $errstr, 2);
The error supression operator will work for any command. It is almost necessary (in my opinion) with things like mysql_connect statements, because the error message, should it appear, would give away sensitive information to a user (like database name and username).

Also, you should add "php" to the opening statement "<?php", just for clarity.
 
Hmm but it works without the "@"?

You're sure you're not putting any funny spaces, or commas or something in bad places (There is NO space between the "@" and the "fsockopen()", if that's not clear)?
 
Back
Top