# PHP Server up or Down Script.



## Powermaster (Jun 15, 2003)

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



```
<?
$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!


----------



## michaelsanford (Jun 16, 2003)

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:
	
	



```
$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.


----------



## Powermaster (Jun 16, 2003)

When using error supression operator the page refuses to load (Well at lease in Safari)


----------



## michaelsanford (Jun 16, 2003)

Safari has nothing to do with it.

The page works fine on my computer. What version of PHP are you running?


----------



## michaelsanford (Jun 16, 2003)

And what error message, if any, are you getting?


----------



## Powermaster (Jun 16, 2003)

The Error is:

"Could not open the page ?http://www.powermaster.cc/test.php? because Safari could not load any data for this location."

Ver: 4.3.0


----------



## michaelsanford (Jun 16, 2003)

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)?


----------



## Powermaster (Jun 16, 2003)

```
$fp = @fsockopen('Apple.com', '81', $errno, $errstr, 2);
```

Port 81 to create the error


----------



## michaelsanford (Jun 16, 2003)

EDIT:

http://www.michaelsanford.com/port.php?port=88
http://www.michaelsanford.com/port.php?port=80

http://www.michaelsanford.com/port.phps


----------



## TommyWillB (Jun 19, 2003)

Works for me using Mozilla Firebird...


----------

