note 72674 modified in function.fsockopen by danbrown
| From: | danbrown@php.net | Date: | Sun, 20 Jul 2008 19:35:32 +0000 |
| Subject: | note 72674 modified in function.fsockopen by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-142300@lists.php.net to get a copy of this message | ||
This script checks specific ports so you need to have the correct port open on the server for this
to work.
E.g if i have a windows domain controller and it is servering LDAP then the following would be used
to check it is online:
<?php
chkServer("MyDC", "389");
?>
for a webserver:
<?php
chkServer("MyWebSvr", "80");
?>
etc etc
--------------------------------------------------------
<?php
// check if a server is up by connecting to a port
function chkServer($host, $port)
{
$hostip = @gethostbyname($host); // resloves IP from Hostname returns hostname on failure
if ($hostip == $host) // if the IP is not resloved
{
echo "Server is down or does not exist";
}
else
{
if (!$x = @fsockopen($hostip, $port, $errno, $errstr, 5)) // attempt to connect
{
echo "Server is down";
}
else
{
echo "Server is up";
if ($x)
{
@fclose($x); //close connection
}
}
}
}
?>
--was--
Sorry for multiple submission of this but Added @ to suppress errors and a few exmples:
Also note that this script checks specific ports so you need to have the correct port open on the
server for this to work.
E.g if i have a windows domain controller and it is servering LDAP then the following would be used
to check it is online:
chkServer("MyDC", "389");
for a webserver
chkServer("MyWebSvr", "80");
etc etc
--------------------------------------------------------
// check if a server is up by connecting to a port
function chkServer($host, $port)
{
$hostip = @gethostbyname($host); // resloves IP from Hostname returns hostname on failure
if ($hostip == $host) // if the IP is not resloved
{
echo "Server is down or does not exist";
}
else
{
if (!$x = @fsockopen($hostip, $port, $errno, $errstr, 5)) // attempt to connect
{
echo "Server is down";
}
else
{
echo "Server is up";
if ($x)
{
@fclose($x); //close connection (i dont know if this is needed or not).
}
}
}
}
http://php.net/manual/en/function.fsockopen.php