Show DevBest [PHP] OOP Server Status

BatDev

Active Member
Apr 20, 2012
110
25
You can use this to see if a port is online / offline on an IP / Domain

PHP:
<?php
 
class StatusTest {
   
 
    function GetStatus($ip, $port) {
       
        $status = array("<font color='red'>OFFLINE</font>", "<font color='green'>ONLINE</font>");
        $fp = @fsockopen($ip, $port, $errno, $errstr, 2);
       
        if (!$fp) {
           
            return $status[0];
           
        } else {
           
            return $status[1];
        }
       
       
       
    }
   
}
 
// How to use the class & function
//Variable->Function('IP','PORT');
 
$Status = new StatusTest(); // Declares the class we are using into a variable
$Status->GetStatus('127.0.0.1','80'); // Calls the function we are using will either display online or offline
 
?>
 

BBC

New Member
Feb 4, 2013
7
4
Been looking for something like this for quite some time, some guy at my College said you couldn't do it... look who's wrong dickhead. ;P

Thanks a lot!
 

Matthewza

Posting Freak
Apr 20, 2012
777
77
That works, but I got a problem I put on my Server I put the right IP and Port and it says offline but the Hotel is online? lool I tried my friends Hotel to and it stays offline.
 

Markshall

Русский Стандарт
Contributor
Dec 18, 2010
2,638
2,393
Thread moved to free programs and scripts.

Nicely done, however, I think the idea of having the status results in the function isn't good, I'd prefer to do it this way, plus you don't even need to make it as a class, you can just have it as a bog standard function, but having it as a class does make it look smarter, so for this code snippet I'll code it in OOP.

PHP:
<?php
class Server {
    public function Status($Server, $Port) {
        $Status = @fsockopen($Server, $Port, $errno, $errstr, 2);
       
        return $Status;
    }
}
 
$Server = new Server();
 
$MyStatus = $Server->Status('127.0.0.1', 80);
if ($MyStatus) {
    echo '127.0.0.1 is online.';
}else{
    echo '127.0.0.1 is offline :(.';
}
?>

Doing it that way means you can set your own success/error messages and don't have to be tied down to the ones you set in your function, if that makes sense?

But still, nicely done.
 

BatDev

Active Member
Apr 20, 2012
110
25
Doing it that way means you can set your own success/error messages and don't have to be tied down to the ones you set in your function, if that makes sense?

But still, nicely done.


Yes it does make sense and there is always room for improvement, thanks for this. I didn't think of it that way
 

Users who are viewing this thread

Top