Subscribe to Tutorial Feed

Flash and PHP Bible

The Flash and PHP Bible has been released! The book can be found on Amazon or wherever fine books are sold in your area.

The Flash and PHP Bible has a forum for quick support.

Scriptplayground » tutorials » php » Verify Server is Online Using PHP

Verify Server is Online Using PHP

Create a simple PHP script to verify a server is online.

View an Example of this article before you get started.

Recently I had a need for a script that would verify a server was online. I decided to write a simple function for just that and am now making it into a tutorial for others to benefit from. The overall script is simple, it pings the server and reacts based on the result.

Okay, start by creating the function.

function verifyRemoteServerOnline($server, $port)
{

}

The checking of the server will be accomplished by using fSockOpen which is included in the standard installation of PHP. This will verify the server and port specified is online.

function verifyRemoteServerOnline($server, $port=null)
{
	if(is_null($port))
	{
		$port = 80; // default Apache port
	}

	$sock = @fSockOpen($server, $port, $err_no, $err_str, 30);
}

Now once we have the socket established lets see if its valid.

function verifyRemoteServerOnline($server, $port=null)
{
	...
	
	return (!$sock) ? false : true;
}

Thats it, like I said in the beginning, its a pretty simple script.

Here is the full script.

function verifyRemoteServerOnline($server, $port=null)
{
	if(is_null($port))
	{
		$port = 80 // default Apache port
	}

	$sock = fSockOpen($server, $port, $err_no, $err_str, 30);
	
	return (!$sock) false : true;
}

Here is a quick example.

$server = "scriptplayground.com";
$port = 80;

if(verifyRemoteServerOnline($server, $port))
{
	print $server . " is online.";
}
else
{
	print $server . " seems to be offline. May need further investigation.";
}

I hope you enjoyed this tutorial. As always, if you have comments or questions, please post them below.

Follow Scriptplayground on Twitter (@scriptplay)

Comments: Verify Server is Online Using PHP

No Comments have been made. Wanna be the first?
Add a comment
The ability to add new comments has been disabled in this archived version.

Loading
Main | Tutorials | Articles