G
G
GM2mars2014-05-08 15:18:01
PHP
GM2mars, 2014-05-08 15:18:01

What are the quick and reliable ways to check the availability of a site in PHP?

It is required to check the site by url whether it is available now or not.
Now I'm using this script:

function accessSite($url) {
  if (preg_match('/[^0-9a-z-_A-Z:\.]/is', $url)!=0) {
    include("modules/idna_convert.class.php");
    $IDN=new idna_convert(array('idn_version' => '2008'));
    $url=$IDN->encode($url);
  }
  @$status=get_headers($url);
  if ($status) {
    if (in_array("HTTP/1.1 200 OK", $status) || in_array("HTTP/1.0 200 OK", $status)) return true;
  }
  return false;
}

But sometimes it happens that it takes a very long time to check the availability and several times the script gave the wrong result.
ps idna_convert.class.php - conversion of Russian domains.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander Kubintsev, 2014-05-08
@GM2mars

It is necessary to determine the accessibility criteria:
1) exceeding the connection timeout;
2) getting some expected response via http.
The request can be made via curl and set a timeout in it.

C
cmx, 2014-05-08
@cmx

The simplest is to get the 200th header from the site.
You can also check the dns record using checkdnsrr()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question