LATEST >>

Welcome Here And Thanks For Visiting. Like Us On Facebook...

EXEIdeas – Let's Your Mind Rock » HTML-CSS-PHP-JavaScript / PHP Codes » How To Check A Server Is Online Or Not Using PHP?

How To Check A Server Is Online Or Not Using PHP?

How-To-Check-A-Server-Is-Online-Or-Not-Using-PHP
Many apps and online tools that are working through a server always need a checking script to check that is the server running or not before procesing the rest opf codes. So for this purpose there are many tools and script. Like if you want to check your server status from your apps ot tools user side the you can use JavaScript that we shared in our last post under Check Server/Website Is Online Or Offline Via Pure JavaScript.

Now if your users is one a JavaScript blocker browser then you can check it via PHP that have many different codes. So here we have a collection of codes in PHP that will check that your server is online or not. So now have a look on below codes and pick up your desire. All are tested and working perfectly. There are URL, IP, And Post option also so they are full featured codes and ready to use.

How To Use:

Just add below codes in you PHP file and change XX.XX.XX.XX with your server IP, XX with your server open Post or http://XX.XX.XX.XX:XX/index.html with your server main page URL. Rest is all done. You can more edit these codes with your desire.

CODE 1:

<?php
$site = "XX.XX.XX.XX";
$port = XX;
$fp = fsockopen($site,$port,$errno,$errstr,10);
if ($fp === false) { 
 print($errno." : ".$errstr); 
}  
if(!$fp)
{
echo "SERVER IS DOWN";
}
else
{
echo "SERVER IS UP";
fclose($fp);
}
?>

CODE 2:

<?php
$url= 'http://XX.XX.XX.XX:XX/index.html';
$array = get_headers($url);
$string = $array[0];
if(strpos($string,"200"))
  {
    echo 'SERVER IS UP';
  }
  else
  {
    echo 'SERVER IS DOWN';
  }
?>

CODE 3:

<?php 
$url= 'http://XX.XX.XX.XX:XX/index.html';
function is_valid_url($url) 
{ 
    $url = @parse_url($url); 
    if (!$url) 
    { 
        return false; 
    } 
    $url = array_map('trim', $url); 
    $url['port'] = (!isset($url['port'])) ? 80 : (int)$url['port']; 
    $path = (isset($url['path'])) ? $url['path'] : '/'; 
    $path .= (isset($url['query'])) ? "?$url[query]" : ''; 

    if (isset($url['host']) AND $url['host'] != gethostbyname($url['host'])) 
    { 
        if (PHP_VERSION >= 5) 
        { 
            $headers = implode('', get_headers("$url[scheme]://$url[host]:$url[port]$path")); 
        } 
        else 
        { 
            $fp = fsockopen($url['host'], $url['port'], $errno, $errstr, 30); 

            if (!$fp) 
            { 
                return false; 
            } 
            fputs($fp, "HEAD $path HTTP/1.1\r\nHost: $url[host]\r\n\r\n"); 
            $headers = fread($fp, 4096); 
            fclose($fp); 
        } 
        return (bool)preg_match('#^HTTP/.*\s+[(200|301|302)]+\s#i', $headers); 
    } 
    return false; 
} 
?> 

CODE 4:

<?php
$url= 'http://XX.XX.XX.XX:XX/index.html';
function check_if_url_exists($url) {
    $headers = @get_headers($url);
    return is_array($headers) ? preg_match('/^HTTP\/\d+\.\d+\s+2\d\d\s+.*$/',$headers[0]) : false;
}
?>

CODE 5:

<?php
$url= 'http://XX.XX.XX.XX:XX/index.html';
function check_if_url_exists($url) {
    if (!$fp = curl_init($url)) return false;
    return true;
}
?>

CODE 6:

<?php

       if (isDomainAvailible('http://XX.XX.XX.XX:XX/index.html'))
       {
               echo "SERVER IS UP";
       }
       else
       {
               echo "SERVER IS DOWN";
       }

       //returns true, if domain is availible, false if not
       function isDomainAvailible($domain)
       {
               //check, if a valid url is provided
               if(!filter_var($domain, FILTER_VALIDATE_URL))
               {
                       return false;
               }

               //initialize curl
               $curlInit = curl_init($domain);
               curl_setopt($curlInit,CURLOPT_CONNECTTIMEOUT,10);
               curl_setopt($curlInit,CURLOPT_HEADER,true);
               curl_setopt($curlInit,CURLOPT_NOBODY,true);
               curl_setopt($curlInit,CURLOPT_RETURNTRANSFER,true);

               //get answer
               $response = curl_exec($curlInit);

               curl_close($curlInit);

               if ($response) return true;

               return false;
       }
?>

CODE 7:

<?php
function Visit($url){
       $agent = "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)";$ch=curl_init();
       curl_setopt ($ch, CURLOPT_URL,$url );
       curl_setopt($ch, CURLOPT_USERAGENT, $agent);
       curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
       curl_setopt ($ch,CURLOPT_VERBOSE,false);
       curl_setopt($ch, CURLOPT_TIMEOUT, 5);
       curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, FALSE);
       curl_setopt($ch,CURLOPT_SSLVERSION,3);
       curl_setopt($ch,CURLOPT_SSL_VERIFYHOST, FALSE);
       $page=curl_exec($ch);
       //echo curl_error($ch);
       $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
       curl_close($ch);
       if($httpcode>=200 && $httpcode<300) return true;
       else return false;
}
if (Visit("http://XX.XX.XX.XX:XX/index.html"))
       echo "SERVER IS UP"."n";
else
       echo "SERVER IS DOWN";
?>

Last Words:

These are the codes that we have. If you have any other better code that is not listed here you can share it with out readers. So now be with us as we will keep updating this posts with new codes too.

You Like It, Please Share This Recipe With Your Friends Using...

Be the first to write a comment.

Leave a Reply

Your email address will not be published. Required fields are marked *