Daniel Scocco
Daniel Scocco

Reputation: 7266

How to silence the warning of a single PHP statement?

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.

Upvotes: 3

Views: 5883

Answers (7)

Ivoglent Nguyen
Ivoglent Nguyen

Reputation: 504

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/

Upvotes: 0

Sachin
Sachin

Reputation: 48

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

Upvotes: 0

alxbrd
alxbrd

Reputation: 1705

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

Upvotes: 0

Christofer Eliasson
Christofer Eliasson

Reputation: 33865

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.

Upvotes: 4

Stelian Matei
Stelian Matei

Reputation: 11623

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'; 
 }

Upvotes: 13

Sarfraz
Sarfraz

Reputation: 382616

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:

Upvotes: 5

p.g.l.hall
p.g.l.hall

Reputation: 1961

Try this:

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

Upvotes: 2

Related Questions