3

I am trying to check whether a certain domain is live or not. My idea is to read the content with file_get_contents(), and check whether it succeed or failed.

$line = file_get_contents('http://www.domain.com'); 
if ($line==false)
    echo 'Domain is dead';
else
    echo 'Domain is live';

The problem I am having is that when it fails, it outputs a warnings on the web page. Turning off all warnings via the PHP config is not an option cause I need them on some other parts. Is there a way to make this single statement not output a warning?

Or is there a better way to check if a domain is live? I tried checkdnsrr() but it was pretty slow.

Daniel Scocco
  • 7,036
  • 13
  • 51
  • 78
  • 2
    Checkout the error control operator http://us2.php.net/manual/en/language.operators.errorcontrol.php - related question http://stackoverflow.com/a/1454592/138383 – Aaron W. Feb 13 '12 at 14:29
  • @DanielS no, actually it's not what you need. People are just answering what *you think you need* without seeing what you are actually trying to do. – Gordon Feb 13 '12 at 14:34
  • I don't want to rock the boat here but never use the error suppression operator. Check [this](http://stackoverflow.com/a/2280413/871580) or [this](http://stackoverflow.com/a/982045/871580) – CBusBus Feb 13 '12 at 14:37
  • @Gordon, actually it is what I needed. I won't copy and paste any answer, but now I know where to look into the PHP manual and what features are available. After that I'll study the material, draw my own conclusions and apply it to my project the most suitable way. – Daniel Scocco Feb 13 '12 at 14:37
  • Use error logging instead of displaying them. Also decide about the level. Next to that, you have more fine-grained control in connecting to a host with the curl library (which is available as a PHP extension). You could control connection timeout etc. to prevent a long waiting script. For both issues you should find existing questions / answers already which should offer more detail (instead of quick thrown answers in here). – hakre Feb 13 '12 at 14:41
  • @DanielS i'd suggest to have a look at http://php.net/manual/en/ref.network.php instead. Error suppression is basically asking how to shoot yourself in the foot… without screaming. – Gordon Feb 13 '12 at 15:51

7 Answers7

13

Use @ sign to suppress warnings:

$line = @file_get_contents('http://www.domain.com');

You could use fopen instead and check if it is null:

 $fp = fopen('http://www.domain.com', 'r');
 if($fp) { 
    echo 'Domain is live'; 
 }
Stelian Matei
  • 11,553
  • 2
  • 25
  • 29
5

You can use suppression operator @.

Using suppression operator is generally a bad idea from developr's perspective. You should use it only in worst-case scenarios.

Whenever possible, try do find an alternative which does not produce errors out of control.

You should also check out:

Sarfraz
  • 377,238
  • 77
  • 533
  • 578
4

You can surpress PHP errors with the @ sign.

PHP: Error Control Operators

Notice the comments on the PHP-manual about performance when using @:

Be aware that using @ is dog-slow, as PHP incurs overhead to suppressing errors in this way. It's a trade-off between speed and convenience.

Christofer Eliasson
  • 32,939
  • 7
  • 74
  • 103
2

Try this:

$line = @file_get_contents('http://www.domain.com'); 
p.g.l.hall
  • 1,961
  • 2
  • 15
  • 26
0

Avoid the use of Error Suppression operator(@) whenever you can avoid. If you try following code it is still having issue in your case.

if ( fopen('http://www.google.com/', 'r')) {
     $line = file_get_contents( 'http://www.google.com/' ); 
     if ( $line==false )
          echo 'Domain is dead';
      else
          echo 'Domain is live';
}
else {
    echo 'Domain not exists';
}

If this domain is not exist then it will again through the warnings. Warning: fopen(): php_network_getaddresses: gethostbyname failed. For your case you can use @. I also suppose this is not best approach to check domain name is alive. I found one script please try it out.

https://github.com/HelgeSverre/Domain-Availability

Sachin
  • 48
  • 6
0

You should not fully download that page (for speed purpose). Just check with HEAD method :

$url = 'http://example.com/';
$code = FALSE;
$options['http'] = array(
    'method' => "HEAD", 
    'follow_location' => 0
);
$context = stream_context_create($options);
file_get_contents($url, NULL, $context);
if (!empty($http_response_header))
    echo 'Domain is live';
else echo 'Domain is dead';

see https://hakre.wordpress.com/2011/09/17/head-first-with-php-streams/

Ivoglent Nguyen
  • 504
  • 3
  • 9
0

Use the error suppressor: http://php.net/manual/en/language.operators.errorcontrol.php

alxbrd
  • 1,675
  • 1
  • 15
  • 16