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

I'm using the above program to get the status of site. But I always get an offline message. Is there any mistake with the code?

1
  • Remove the error suppression operator (@). And what is the contents of errno and errstr` ? Commented Mar 22, 2012 at 5:48

9 Answers 9

35

The hostname does not contain http://, that is only the scheme for an 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.';
}
?>
34

What 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"; }
7

I know this is an old post, but you could also parse the output of:

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

That would give you the full HTTP status.

1
  • The accepted answer didn't work. Yours did. Thank you.
    – VE7JRO
    Commented Oct 31, 2023 at 22:41
3

This can work faster

<?php
$domain = '5wic.com';
if(gethostbyname($domain) != $domain )
{
echo "Up";
}
else
{
echo "Down";
}
?>
1
  • 4
    Unfortunately, this is not the optimum solution. When the server down, this may still say the website is up for some time, due to the DNS resolution. I would go with @stewe's solution above. And there are more implications as well, please check the php website php.net/manual/en/function.gethostbyname.php . Thanks Commented Mar 20, 2017 at 10:48
1

should be

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

not

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

but curl would be better

1
  • 1
    When I tried the above code I get the following error: Warning: fsockopen(): unable to connect to google.com:80 (Unable to find the socket transport "http" - did you forget to enable it when you configured PHP?) /stchecker.php on line 4 offline.
    – joHN
    Commented Mar 22, 2012 at 5:56
1

You can use this:

function is_on($domain) {
  if(gethostbyname($domain) === gethostbyname("testillegaldomain.tld")) {
    return false;
  } else {
    return true;
  }
}

This gets the IP address of a domain that doesn't exist and compares it to your domain's IP; if it is equal, then the site is down, else its on.

Also, don't include https:// prefix when using this function!

1

I am using the response status code for this. This will returns true for live & temporary not available sites.

public function checkLive($url)
{
    $ch = curl_init();
    curl_setopt ($ch, CURLOPT_URL, $url);
    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt ($ch, CURLOPT_TIMEOUT, 15);
    $http_respond = trim(strip_tags(curl_exec($ch)));
    $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);

    curl_close($ch);
    if (in_array($http_code, ["200", "302"])) {
        return true;
    } else {
        return false;
    }
}
1
  • This worked for me, specially because hosting providers might redirect to a 302 page. Commented Mar 30, 2022 at 23:28
0

This is a quicker way than fsock.

$url = 'https://google.com';
$url = gethostbyname(str_replace(['https://','http://'],NULL,$url));
if(filter_var($url,FILTER_VALIDATE_IP)) {
   // online!
}
else {
   // offline :(
}
0
 /* --Start-- Determine that url is exists or not */
function urlExistsOrNot($url) {
    if (filter_var($url, FILTER_VALIDATE_URL)):
        $curl = curl_init($url);
        curl_setopt_array($curl, [
            CURLOPT_FOLLOWLOCATION => TRUE, /* we need the last redirected url */
            CURLOPT_NOBODY => TRUE, /* we don't need body */
            CURLOPT_HEADER => FALSE, /* we don't need headers */
            CURLOPT_RETURNTRANSFER => FALSE, /* we don't need return transfer */
            CURLOPT_SSL_VERIFYHOST => FALSE, /* we don't need verify host */
            CURLOPT_SSL_VERIFYPEER => FALSE/* we don't need verify peer */
        ]);
        /* Get the HTML or whatever is linked in $url. */
        curl_exec($curl);
        $httpCode = curl_getinfo($curl, CURLINFO_EFFECTIVE_URL); /* Try to get the last url */
        $httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE); /* Get http status from last url */
        unset($url, $handle);
        curl_close($handle);
        if ($httpCode == 200)
            return "Online";
        return "Offline";
    else:
        return "Invalid Url";
    endif;
}
/* --End-- Determine that url is exists or not */

/* Example */
urlExistsOrNot('https://google.com');
1

Not the answer you're looking for? Browse other questions tagged or ask your own question.