55502f40dc8b7c769880b10874abc9d0

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.

<?

# 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 !

D41d8cd98f00b204e9800998ecf8427e

Casper

May 6, 2010, May 06, 2010 13:56, permalink

No rating. Login to rate!

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".

Ad19166b458abb9d25aa0d0c74eb5d1f

Mathew Spearey

May 22, 2010, May 22, 2010 19:33, permalink

No rating. Login to rate!

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" ;
} 

?>
Ad19166b458abb9d25aa0d0c74eb5d1f

Mathew Spearey

May 22, 2010, May 22, 2010 19:34, permalink

No rating. Login to rate!

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" ;
} 

?>
74002eeb5435bacbda9bf392756d5033

El Barto

July 11, 2010, July 11, 2010 14:55, permalink

No rating. Login to rate!

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();
} 

?>
74002eeb5435bacbda9bf392756d5033

El Barto

July 11, 2010, July 11, 2010 14:58, permalink

No rating. Login to rate!

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.

F2c5dc83424e10e2315a3d4b1b647302

Bedava

July 31, 2010, July 31, 2010 11:06, permalink

No rating. Login to rate!

Bedava her şey oyna oyun müzik dinle

9e166329e042ef17cd6a62c97f2d2f09

MonsterKiller

November 16, 2010, November 16, 2010 23:33, permalink

No rating. Login to rate!

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);
36edeff11dc3530ce93c4b2e6ab207f3

Zach

September 8, 2011, September 08, 2011 16:05, permalink

No rating. Login to rate!

I would love to know how to make this an array of domain names instead of just one.

36edeff11dc3530ce93c4b2e6ab207f3

Zach

September 8, 2011, September 08, 2011 16:05, permalink

No rating. Login to rate!

I would love to know how to make this an array of domain names instead of just one.

36edeff11dc3530ce93c4b2e6ab207f3

Zach

September 8, 2011, September 08, 2011 16:05, permalink

No rating. Login to rate!

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();
} 

?>

Your refactoring





Format Copy from initial code

or Cancel