<?
# Domain Name Variable
$domainName = "http://www.refactormycode.com" ;
# Function
function DomainCheck($domainName){
$startTime = microtime(true);
$openDomain = fsockopen ($domainName, 80, $errno, $errstr, 10);
$finishTime = microtime(true);
$serverStatus = 0;
# -> If Condition
if (!$openDomain) $serverStatus = -1;
else {
fclose($openDomain);
$status = ($finishTime - $startTime) * 1000;
$serverStatus = floor($serverStatus);
}
return $serverStatus;
}
$serverStatus = DomainCheck($domainName);
if ($serverStatus != -1)
{
echo "Server is offline" ;
} else {
echo "Server is Online" ;
}
?>
Refactorings
No refactoring yet !
Casper
May 6, 2010, May 06, 2010 13:56, permalink
You only check if the port is open.. The better way (in my opinion) would be to also test if the service that is listening on that port responses as expected. You could check against the first line of the http response header, e.g. "HTTP/1.1 200 OK".
Mathew Spearey
May 22, 2010, May 22, 2010 19:33, permalink
As Casper said check what the response header says, also you can save a line or so of that if statement on line 13/14, leave the $serverStatus as zero, then just check if $openDomain is valid. if it is change $serverStatus.
remember that zero will always be seen as false, and anything else as true.
<?
// Domain Name Variable
$domainName = "http://www.refactormycode.com" ;
// Function
function DomainCheck($domainName)
{
$startTime = microtime(true);
$openDomain = fsockopen ($domainName, 80, $errno, $errstr, 10);
$finishTime = microtime(true);
$serverStatus = 0;
if ($openDomain)
{
fclose($openDomain);
$status = ($finishTime - $startTime) * 1000;
$serverStatus = floor($serverStatus);
}
return $serverStatus;
}
$serverStatus = DomainCheck($domainName);
if (!$serverStatus)
{
echo "Server is offline" ;
}
else
{
echo "Server is Online" ;
}
?>
Mathew Spearey
May 22, 2010, May 22, 2010 19:34, permalink
As Casper said check what the response header says, also you can save a line or so of that if statement on line 13/14, leave the $serverStatus as zero, then just check if $openDomain is valid. if it is change $serverStatus.
remember that zero will always be seen as false, and anything else as true.
<?
// Domain Name Variable
$domainName = "http://www.refactormycode.com" ;
// Function
function DomainCheck($domainName)
{
$startTime = microtime(true);
$openDomain = fsockopen ($domainName, 80, $errno, $errstr, 10);
$finishTime = microtime(true);
$serverStatus = 0;
if ($openDomain)
{
fclose($openDomain);
$status = ($finishTime - $startTime) * 1000;
$serverStatus = floor($serverStatus);
}
return $serverStatus;
}
$serverStatus = DomainCheck($domainName);
if (!$serverStatus)
{
echo "Server is offline" ;
}
else
{
echo "Server is Online" ;
}
?>
El Barto
July 11, 2010, July 11, 2010 14:55, permalink
I changed to use Exceptions, because it's more OOP-friendly and allows you to pass information about what was the error. I also introduced the possibility of checking if it's possible to write to the socket (you could also add some checking for the response).
Lastly, I did some cosmetic changes to adjust to Zend's best practices guide, which you can take or leave.
<?php
# Domain Name Variable
$domainName = "www.refactormycode.com" ;
# Function
function domainCheck($domainName, $attemptRequest=false)
{
$errorno = 0;
$errorstr = "";
$fp = fsockopen ($domainName, 80, $errno, $errstr, 10);
if (!$fp) {
throw new Exception(sprintf("Failed to connect to host %s (%d): %s",
$domainName, $errno, $errstr));
} else {
if ($attemptRequest) {
$req = "GET / HTTP/1.1\r\n";
$req .= "Host: {$domainName}\r\n";
$req .= "Connection: Close\r\n\r\n";
if (fwrite($fp, $req) == false) {
throw new Exception("Failed to send data to {$domainName}");
}
}
fclose($fp);
}
return true;
}
try {
if (domainCheck($domainName)) {
echo "Server is Online";
}
} catch (Exception $e) {
echo "Server is offline: " . $e->getMessage();
}
?>
El Barto
July 11, 2010, July 11, 2010 14:58, permalink
I noticed I made a mistake on lines 9 and 10. They should be the names of the variables passed to fsockopen. It's important to define them before using them, because "explicit is better than implicit" and because in some environments (error_reporting = E_ALL) you would get warning messages.
MonsterKiller
November 16, 2010, November 16, 2010 23:33, permalink
I would use @fsockopen instead to prevent it showing horrible error messages to users when the service is not online.
$fp = @fsockopen ($domainName, 80, $errno, $errstr, 10);
Zach
September 8, 2011, September 08, 2011 16:05, permalink
I would love to know how to make this an array of domain names instead of just one.
Zach
September 8, 2011, September 08, 2011 16:05, permalink
I would love to know how to make this an array of domain names instead of just one.
Zach
September 8, 2011, September 08, 2011 16:05, permalink
I would love to know how to make this an array of domain names instead of just one.
<?php
# Domain Name Variable
$domainName = "67.137.21.162:2000" ;
# Function
function domainCheck($domainName, $attemptRequest=false)
{
$errorno = 0;
$errorstr = "";
$fp = fsockopen ($domainName, 80, $errno, $errstr, 10);
if (!$fp) {
throw new Exception(sprintf("Failed to connect to host %s (%d): %s",
$domainName, $errno, $errstr));
} else {
if ($attemptRequest) {
$req = "GET / HTTP/1.1\r\n";
$req .= "Host: {$domainName}\r\n";
$req .= "Connection: Close\r\n\r\n";
if (fwrite($fp, $req) == false) {
throw new Exception("Failed to send data to {$domainName}");
}
}
fclose($fp);
}
return true;
}
try {
if (domainCheck($domainName)) {
echo "Server is Online";
}
} catch (Exception $e) {
echo "Server is offline: " . $e->getMessage();
}
?>
Here's server status checker code in Php.
I'm waiting for your refactorings.
- - -
Take a look also http://www.cilekoyun.com and http://www.ikikisilikoyunlar.net have a rest for while.
Thanks, regards.