Get site status - up or down

<?php
$host = 'http://google.com';
if($socket =@ fsockopen($host, 80, $errno, $errstr, 30)) {
echo 'online!';
fclose($socket);
} else {
echo 'offline.';
?>

      

I am using the above program to get site status. But I always get the offline message. Is there a bug with the code?

+3


source to share


5 answers


The hostname does not contain http://

, it is only the scheme for the URI.

Remove it and try this:



<?php
$host = 'google.com';
if($socket =@ fsockopen($host, 80, $errno, $errstr, 30)) {
echo 'online!';
fclose($socket);
} else {
echo 'offline.';
}
?>

      

+11


source


How about a curl solution?



function checkOnline($domain) {
   $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;
}
if(checkOnline('http://google.com')) { echo "yes"; }

      

+17


source


I know this is an old post, but you can parse the output as well:

$header_check = get_headers("http://www.google.com");
$response_code = $header_check[0];

      

This will give you the full HTTP status.

+3


source


It can run faster

<?php
$domain = '5wic.com';
if(gethostbyname($domain) != $domain )
{
echo "Up";
}
else
{
echo "Down";
}
?>

      

+3


source


it should be

if(fsockopen($host, 80, $errno, $errstr, 30)) {
...

      

not

if($socket =@ fsockopen($host, 80, $errno, $errstr, 30)) {
...

      

but the curl will be better

+1


source







All Articles