How to see php errors

How to Display and Log PHP Errors

There are two methods for viewing PHP errors that occur while running your website. You can either display errors directly on your website (viewable from any web browser) or enable error logging to write the errors to a specified file (viewable inside a text file). This article will cover how to turn display_errors On and Off, adjust error reporting settings, configure error logging, and use the ini_set() function to help troubleshoot PHP errors with your website.

WARNING: These instructions were written for the EasyApache 3 PHP/Apache configuration. This older configuration efficiently handles direct modifications to the php.ini file. However, if you are running EasyApache 4, then cPanel/WHM is designed to maintain the php.ini file(s). Any changes made directly to the php.ini file (while using EA3) may cause this functionality to not perform as expected. Be sure to check which version of EasyApache you are running, prior to making modifications as outlined in this article.

Editing the php.ini to Display Errors

While your site is live, the php.ini file should have display_errors disabled for security reasons. However, for the development environment, display_errors can be enabled for troubleshooting. Displaying errors should be disabled while the site is live, to protect sensitive information and not interfere with the format of your website pages. When display_errors is On, your site’s pages will display any PHP errors encountered when loading your website in a browser. This section will explain how to control error reporting and turn display_errors On and Off.

  1. Log into your cPanel.
  2. Go to the File Manager. Select the home directory for your website (by default: public_html) and click Go.
  3. Find the “Error handling and logging ” section in the php.ini. In order to display or log errors, you need to enable error_reporting by removing the ( ; ) from in front to the line. You can disable error_reporting by adding a ( ; ) in front of the line. Refer to the code below: Error reporting disabled:
; - Show all errors, except for notices ; ;error_reporting = E_ALL & ~E_NOTICE ;
; - Show all errors, except for notices ; error_reporting = E_ALL & ~E_NOTICE ;

Next you can set the display_errors variable to On or Off to either show the errors on your website or not. Look for the display_errors line in the php.ini file and set it to On to display errors or Off to turn not display errors. The code looks like the following:Display errors:

php.ini Error Reporting Settings

PHP has a list of different error reporting settings within the php.ini file itself. For example, if you want to display only warnings you can change the error_reporting line to the following:

error_reporting = E_WARNING

The following table was created from the settings found in a standard php.ini file. Refer to the following table for the available options.

List of available options taken from php.ini Description
E_ALL All errors and warnings
E_ERROR fatal run-time errors
E_WARNING run-time warnings (non-fatal errors)
E_PARSE compile-time parse errors
E_DEPRECATED notices for the use of functions that will be retired in a future version
E_NOTICE run-time notices (these are warnings which often result from a bug in your code, but it’s possible that it was intentional (e.g., using an uninitialized variable and relying on the fact it’s automatically initialized to an empty string)
E_CORE_ERROR fatal errors that occur during PHP’s initial startup
E_CORE_WARNING warnings (non-fatal errors) that occur during PHP’s initial startup
E_COMPILE_ERROR fatal compile-time errors
E_COMPILE_WARNING compile-time warnings (non-fatal errors)
E_USER_ERROR user-generated error message
E_USER_WARNING user-generated warning message
E_USER_NOTICE user-generated notice message

Turning Error Logging On

By default errors are written to the error_log, which is set to /dev/null. This means, error logging won’t occur. When errors are turned on, errors will be stored in a file in the directory the error occurs in. For example, if you have a PHP file called index.php in a subdirectory like public_html/wordpress, if you have any PHP errors while running that file’s scripts, the error log will be stored in that folder (public_html/wordpress). You can specify (in the php.ini) which file to store all errors in.

Important! You must place the following code in the .htaccess to make the local php.ini work for the current directory where the .htaccess resides and all subdirectories within.

 suPHP_ConfigPath /home/USERNAME/public_html 

The IfModule causes Apache to load the directive only if suPHP is used, so the site doesn’t break if switched to another PHP handler such as DSO.

For more information on suPHP and DSO please see our article on Choosing the best PHP handler.

  1. Log into your cPanel.
  2. Go to the File Manager. Select the public_html directory and click Go.
  3. You can set the following line of code to On to log errors or Off to turn error logging off.
; Log errors to specified file. error_log = error_log

This will write all errors to the error_log file inside the public_html directory, regardless of what directory the errors were encountered in:

; Log errors to specified file. error_log = /home/userna5/public_html/error_log

Using int_set() to Display Errors

In the case you want errors to not display site wide and you want to check errors on a single page, you can use the ini_set() function to have errors displayed on a particular page. The basic syntax from php.net shows the function and its parameters is as follows:

string ini_set ( string $varname , string $newvalue )

This can be placed at the top of your PHP page, with the error_reporting variable in it, to allow error checking for that particular page. Below are the steps for how to do this.

  1. Log into your cPanel.
  2. Go to the File Manager. Select the public_html directory and click Go.
  3. Navigate to the PHP file you want to check errors for. Open the file in the code editor.
  4. In the file, add the following at the top.

Maintain Your Log Files

Now that you have enabled error logging, be sure to maintain your log files. If you are getting errors regularly, be sure to remove the logs periodically. You may do so by just removing the file from within the File Manager or through any other method that you prefer to manage your files. A large log file can sometimes cause issues by possibly filling your disk space or if on shared hosting with unlimited disk space, begin to impact other customers on the server.

Thoughts on “ How to Display and Log PHP Errors ”

Hi, I read the document to edit the php.ini, however, I couldnt find this file in my instalation in the folder public_html. So, I just created it and place the changes that I would like in the setup of PHP. Is that right? Thanks, Otto.

Depending on your cPanel version, you may need to use cPanel to modify the php.ini file to customize it.

Hello, I got an HTTP ERROR 500 from the website i designed using joomla and i can’t figure out how to solve this problem. Kindly help out

I’m sorry to see that your website is displaying an HTTP 500 Error. Were you able to find an error message with more details by following the instructions in the guide above? Those details would be necessary to diagnose and find a solution to resolve the error. If you are a customer of InMotion Hosting I recommend contacting our Live Technical Support for more private/personal assistance.

Error

Источник

Читайте также:  Теги для html основные
Оцените статью