3

I've got a pretty simple page which fetches a url and parse some data.

I have built into my page some error handling in the event that the response is a 404 error.

However, I can't seem to stop php from spitting out the following errors

 Warning: file_get_contents(http://url-to-retrieve.com/123.html) [function.file-get-contents]: failed to open stream: HTTP request failed! HTTP/1.1 404 Not Found in /var/www/html/maickl/procs/get.php on line 84

Notice: Undefined offset: 0 in /var/www/html/maickl/procs/get.php on line 91

I start the page with

error_reporting(0)

Any suggestions as to why this is happening, why these errors are not being suppressed (it seems to be only on this page), and what I can do about it?

4 Answers 4

8

Using error_reporting(0); shoud disable error reporting, which means you should not get that error -- are you sure it's not re-enabled somewhere ?


But what you are probably looking for, actually, is not to change [`error_reporting`][1], but to disable [`display_errors`][2], which can be done using some code like this one :
ini_set('display_errors', 'Off');

With this :

  • errors / warnings will not be displayed on your website, which is nice for end-users (technical errors should never be displayed)
  • but error_reporting is not disabled, which means errors can/will still be logged -- see log_errors and error_log.
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks Pascal, I thought that should hide all errors, but it isn't and I'm not even sure how I would 're-enable' error_reporting. I tried using the display_errors as you provided, but I'm still getting the same responses. Very strange....
Sorry about that, I got it. I was including a file which for some reason had a different error setting. thanks
OK :-) Thanks for your comment (I have to admit the first was let me wondering... Nice you posted the second one too ^^ ). Have fun !
7

Also remember that if you have set a user defined error handler function, then error_reporting() settings are completely ignored.

1 Comment

This should have been a comment.
3

Turning off error reporting is one way of doing it, but I would suggest simply fixing the error or adding appropriate error handling.

try {
     //try to do the following
     $content = file_get_contents('file/to/try.txt');
} catch (Exception $e) {
     //do this in case of an error
     die($e->getMessage());
}

Comments

0

Ensure that you're not forgetting output by debug_print_backtrace(). You can still capture it and prevent it from being output to the client using the following:

ob_start(); 
$error = mysqli_real_escape_string($db, $error.print_r(debug_print_backtrace(),1));
ob_end_clean(); 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.