clubmate.fi

A good[ish] website

Web development blog, loads of UI and JavaScript topics

How to easily view and log PHP errors

Filed under: php— Tagged with: .htaccess, error handling, snippets

When developing a site the visible PHP errors are extremely handy. But, on a production site they may give out too much information or it's just plain bad habit to show that crap to users.

I like to use the .htaccess method, it's simple and effective.

First, make a file called php_error.log in to the root of your WP (or whatever it is you're using) install. Then paste the following to your .htaccess file, that also should reside the root.

For production site:

# Log PHP errors
# Display no errorss to user
php_flag display_startup_errors off
php_flag display_errors off
php_flag html_errors off
# log to file
php_flag log_errors on
php_value error_log php_error.log

And for dev site, you can turn the errors on (to show in the browser), so you don't always have to consult the log file for errors:

# Log PHP errors
# display errors to users
php_flag display_startup_errors on
php_flag display_errors on
php_flag html_errors on
# log to file
php_flag log_errors on
php_value error_log php_error.log

Comments would go here, but the commenting system isn’t ready yet, sorry. Tweet me @hiljaa if you want to make a correction etc.

  • © 2021 Antti Hiljá
  • About
  • Follow me in Twatter → @hiljaa
  • All rights reserved yadda yadda.
  • I can put just about anything here, no one reads the footer anyways.
  • console.log('Smash the patriarchy!')
  • I love u!