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.

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. Report all PHP errors

    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 ; 

    Error reporting enabled to specifically report all errors, but not notices:

    ;   - Show all errors, except for notices ; error_reporting = E_ALL & ~E_NOTICE ; 


  4. Display PHP errors On

    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:

    display_errors = On 

    Do not display errors:

    display_errors = Off 


php.ini Error Reporting Settings

List of PHP 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
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.

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. Log PHP errors On

    You can set the following line of code to On to log errors or Off to turn error logging off.

    log_errors = On 


  4. Path to PHP Error log

    Next you can save errors from any page in your files to a specific location by specifying the error_log. The example below uses a file named error_log

    This will place the error_log in the directory the error occurs in:

    ; 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.
    ini_set('display_errors', '1');

    1 = On
    0 = Off

    Save the page.

    Now your PHP page only will display errors.

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.

  • 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

    Sorry there was an error sending your form.

    • mail:Could not instantiate mail function.
    •  why is this???????
    • We are happy to help but will need some additional information. Can you provide a link to the form for us to test?

      Thank you,
      John-Paul

  • So the local .htaccess file should include code from any higher-level .htaccess files (IE in my home directory) to retain that functionality?

    • The code does not require repetition. The .htaccess in the higher level is recursive, meaning it will affect directories below it.

    • Hello!

      Thank you for contacting us today. Do you need assistance with your script? Are you having trouble with this script? Are you receiving any errors?

      If you require further assistance, please let us know!

      Sincerely,
      Carlos E

  • In “turning error logging on” you mention adding

    <IfModule mod_suphp.c>
    suPHP_ConfigPath /home/USERNAME/public_html
    </IfModule>

    to the local .htaccess file to to enable local settings. Does this ADD TO or SUPERCEDE the settings in  higher-level .htaccess files (IE in the directories above?)

  • Nice tutorial. This tutorial was really helpful to known the error reporting in PHP. This is helpful to beginners. Thanks.

  • Do I need to setup anything to make php work? It seems the php interpreter works, but it does not work with function calls.

    • Hello Hung Nguyen,

      Sorry for the problem with your PHP functions. The servers are all loaded and ready to run PHP, so you don’t need to do any configuration. If your functions aren’t working, then it may be related to your code. We do not provide coding support so you may be better off posting your code in a forum specifically for that purpose. You may also want to check your PHP configuration in the cPanel for the PHP version being used.

      If you have any further questions or comments, please let us know.

      Regards,
      Arnel C.

  • hi, im using joomla 3.4.8 version for my website and a error seems to be keep on repeating “PHP Notice: Undefined index: HTTP_HOST in /home/novelteamnew/public_html/libraries/joomla/uri/uri.php on line 174”. i dont knw how to handle this? can anyone help with this??

     

    • Hello Guruprasad G,

      Sorry for the problems with Joomla. I tried looking for information on this error and this is what I found: Joomla forum post. It may help with your issue, or at least help get you going in the right direction.

      I hope that helps to answer your question! If you require further assistance, please let us know!

      Regards,
      Arnel C.

  • I am in need of editing my php.ini file. The article says that the php.ini file is located in the public_html directory, but the file isn’t there. Am I looking the wrong place?

  • Hi sir,

    I am php developer , I have some problem I want to know about error log becouse client demand to error log, show error message and save in db with all error detals and remove by user.(means client). how to solve problem .

    • Hello Anant,

      Thanks for the question. We can’t help you with code, but we can try to provide some guidance that will lead to what you need. I found this post in a forum concerning your question:

      “Use set_error_handler(‘myErrorFunction’); (See PHP Manual set_error_handler) define myErrorFunction with database inserts, turn off standard error reporting display with ini_get(‘display_errors’, 0);”

      (How to save PHP errors in Database)

      I hope this helps to answer your question, please let us know if you require any further assistance.

      Regards,
      Arnel C.

    • Hello Sudhanshu Kumar,

      Sorry for the problem. The issue you’re having has been reported in the Joomla Forums here. Your best bet is to follow their suggestions and see if it doesn’t resolve the problem you’re seeing.

      Kindest regards,
      Arnel C.

  • i am sanjay and php developer  but one error 

    i am send mail function code is wright in public_html in any foulder to run this code 

    but error in public_html in my page run mail.php this error 

     PHP Startup: Unable to load dynamic library '/opt/php54/lib/php/extensions/no-debug-non-zts-20100525/homeloader.so' - /opt/php54/lib/php/extensions/no-debug-non-zts-20100525/homeloader.so: cannot open shared object file: No such file or directory in Unknown on line 0
    
    • Hello sanjay,

      It looks like you might want to edit your php.ini file and look for the line that mentions homeloader.so and comment it out by placing a ; in front of that line and then saving it.

      You might want to check with your host if the main PHP mail() function is disabled on your server.

      You might want to use phpMailer to send mail from your website instead using the SMTP authentication that a normal email client would use.

      Please let us know if you have any further questions.

      – Jacob

  • hello friend i am php devloper 

    i one site devlopd but one erro 

    send mail function not work in public_html and i create public_html in any folder in check to work adn run 

    this  code same  in public_html and any foldre 

  • <?php

     if(isset ($_POST[‘user’])){

         $user = $_POST[‘user’];

         $pass = $_POST[‘pass’];

         

         $con = mysql_cnnection(“lochost”,”root”‘””);

           if(!$con){die(Could not connect: ‘.mysql_error());}

      mysql_select_db(“test”,$con);

      if(mysql_num_rows(mysql_query(“SELECT * FROM user where username = ‘$user AND password = ‘$pass'”)))

      {

      $result =mysql_query(“SELECT * FROM users where username =’$user’ AND password =’pass'”);

      while($row =mysql_fetch_array($result))

      {

      $expire =time()+60*60*24*30;

      setcookie(“uid”,$row[‘uid’]$expire));

      }           

         }

        else{

        echo”<br>username or password</b><br><br>”;

        }

        }

        mysql_close($con);

        } 

        echo”<form method = ‘post’>

       Username:<input type=’text’ name=’user’><br>

       Password:<input type=’password’ name=’pass’><br>

       <input type=’submit’ value=’LOGIN’>

       </form>”

       ?>  

    • Hello Rakesh,

      Thank you for contacting us today. Do you need assistance? Are you having trouble with this script? Are you getting any errors?

      If you have any further questions, feel free to post them below.
      Thank you,

      -John-Paul

Leave a Reply to Themesrefinery Cancel reply