Tuesday, 22 August 2017

How to check if port is active OR Not?

How to check if port is active OR Not?

Question: What is fsockopen?
fsockopen — Open Internet or Unix domain socket connection.


Question: How to check if port is open OR Not in particular IP Address OR Domain?
function pingDomainWithPort($domain='localhost',$port=80){
    $starttime = microtime(true);
    $file      = @fsockopen($domain, $port, $errno, $errstr, 10);
    $stoptime  = microtime(true);
    $status    = 0; //in active

    if (!$file) { 
        $status = -1;  // Site is down

    } else {

        fclose($file);
        $status = ($stoptime - $starttime) * 1000;
        $status = floor($status);
    }
    return $status;
}

pingDomainWithPort($_SERVER['REMOTE_ADDR'],1935);
pingDomainWithPort($_SERVER['REMOTE_ADDR'],1458);
pingDomainWithPort($_SERVER['REMOTE_ADDR'],80);
pingDomainWithPort($_SERVER['REMOTE_ADDR'],8080);

You can pass the domain name or Ip Address.