Naveed
Naveed

Reputation: 42093

PHP/Zend: How to force browsers to don't show warnings on webpage for a particular case?

I am trying to get twitter updates like this:

try {

  $doc = new DOMDocument();
  $doc->load('http://twitter.com/statuses/user_timeline/1234567890.rss');
  $isOK = true;

} catch( Zend_Exception $e ) {
  $isOK = false;
}

If there is not problem with internet connection then $isOK = true; is set. But if there is a problem in loading twitter page then it shows following warnings and does not set $isOK = false;

Warning: DOMDocument::load(http://twitter.com/statuses/user_timeline/1234567890.rss) [domdocument.load]: failed to open stream: HTTP request failed! HTTP/1.1 404 Not Found in /var/www/vcred/application/controllers/IndexController.php on line 120

I don't want to see above warning on my webpage in any case. Any idea?

Thanks

Upvotes: 2

Views: 4779

Answers (2)

Gordon
Gordon

Reputation: 316979

Several options:

Suppress all errors for just this function call

@$doc->load('http://twitter.com/statuses/user_timeline/1234567890.rss');

which is the same as

$oldLevel = error_reporting(0);
$doc->load('http://twitter.com/statuses/user_timeline/1234567890.rss');
error_reporting($oldLevel);

Suppressing errors this way is generally frowned upon, as it makes code harder to debug. Like Shrapnel pointed out, you want to disable public display of all error messages on a production system anyway. On Dev systems you are encouraged to use error_reporting(-1);, which would enable E_ALL and E_STRICT.

If you want to use try/catch, you can also change default error handling and convert all errors to exceptions by doing

function myErrorHandler($errno, $errstr, $errfile, $errline) {
    throw new Exception($errstr, $errno);
}
set_error_handler("myErrorHandler");

This is a global change though and affects everything raised. You'd also have to use catch(Exception $e) instead of Zend_Exception then in your code, but it would work. Note that the above would convert everything, even Notices, so you will also get an Exception about $isOk being undefined if you are trying to access this later. Feel free to adapt the handler to your liking and check out the user comments for set_error_handler for more refined versions.

Another global change would be to change the application.ini in your application folder, e.g. letting Zend Framework control error handling:

phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1

Change these to your needs. They are the same as in PHP.ini, e.g.

display_errors:

This determines whether errors should be printed to the screen as part of the output or if they should be hidden from the user.

display_startup_errors:

Even when display_errors is on, errors that occur during PHP's startup sequence are not displayed. It's strongly recommended to keep display_startup_errors off, except for debugging.

Upvotes: 6

Your Common Sense
Your Common Sense

Reputation: 157895

ini_set('display_errors',0);

it should be set this way on any production site.
users shouldn't be allowed to see system error messages at all

Upvotes: 2

Related Questions