Kamis, 03 Oktober 2013

Cek Status Website Menggunakan PHP

Berikut coding php dengan memanfaatkan fungsi curl :

<?php 
/**
 * Function to check a website status
 * @param string url the url of the website to test
 * @return boolean true if the site is up, false otherwise
*/


function checkStatus($url){
     $agent = "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_5_8; pt-pt) AppleWebKit/533.20.25 (KHTML, like Gecko) Version/5.0.4 Safari/533.20.27";


     // initializes curl session
     $ch=curl_init();


     // sets the URL to fetch
     curl_setopt ($ch, CURLOPT_URL,$url );


     // sets the content of the User-Agent header
     curl_setopt($ch, CURLOPT_USERAGENT, $agent);


     // return the transfer as a string
     curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);


     // disable output verbose information
     curl_setopt ($ch,CURLOPT_VERBOSE,false);


     // max number of seconds to allow cURL function to execute
     curl_setopt($ch, CURLOPT_TIMEOUT, 5);


     curl_exec($ch);


     // get HTTP response code
     $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);


     curl_close($ch);


     if($httpcode>=200 && $httpcode<300)
          return true;
     else
          return false;
     }


     // Place this at the very top of script 
     $start = microtime(TRUE);


     if(!function_exists('curl_init')) echo "cURL not installed. Trying something else...";


     $array = array("http://192.168.51.2", 
                    "http://192.168.51.5", 
                    "http://192.168.51.18", 
                    "http://192.168.51.27", 
                    "http://192.168.51.192",
                    "http://192.168.51.215"
                    );
 
     foreach ($array as $value) {
          if(checkStatus($value))
               echo "Website <img src='img/up.png' height=16 width=16> $value is <font color='blue'>up</font><br />\n";
          else
               echo "Website <img src='img/down.png' height=16 width=16> $value is <font color='red'>down</font><br />\n";
     }


     // Place this at the very bottom of script 
     $finish = microtime(TRUE);


     // Subtract the start time from the end time to get our difference in seconds 
     $totaltime = $finish - $start; 
 
     echo "<font size='1'>This script took ".$totaltime." seconds to run</font>"; 
?>



Tidak ada komentar:

Posting Komentar