Mostly in web development, we use PHP as a programming language and it is used widely to create dynamic web pages. Errors generate during creation or sometimes version changes generate errors. Then PHP displays these errors directly on the browser and it may reveal some sensitive information about your site. This can expose vulnerabilities and interrupt users. So fortunately, we have a solution to prevent php errors from being displayed on your browser through the display_errors directive. In this post, I will explain how we can use the display_errors parameter.

So, our focus should be to silence the errors on the live website but keep them easy to troubleshoot during development.

Understanding display_errors

In most cases in PHP is by default configured to display errors directly on the web browser .it can be helpful at the time of the development process for troubleshooting and debugging the issues. but it may reveal sensitive information about your application code infrastructure, making it easier for potential attackers to exploit vulnerabilities.

How to Disable display_errors

Disabling the display_errors directive is a simple process that consists of changing in php configuration settings. Follow these steps to prevent php errors from being displayed on your browser:

Locate the PHP Configuration File:

The location of the PHP configuration file can be found in different ways and the configuration file location depends on your server setup. The default location of the php.ini file is /etc/php.in . But here I am explaining how you can find it through the phpinfo() function. The simple method is to create a PHP file eg. infophp.php with the following content:

<?php
phpinfo();
?>

To upload the file, explore your website server’s document root directory. Then, access the file through your web browser by visiting the website at https://domain_name_or_IP/infophp.php. Once the infophp.php file loads, you’ll find the path to your PHP configuration file displayed under “Loaded Configuration File”.

phpinfo configuration file path

Open the PHP Configuration File and find display_errors :

Use a vi or nano text editor to open the php.ini file. find display_errors directive within the php.ini file. By default, it is set to On and we have to set the value to Off.

  • display_errors = On (By Default): Errors will displayed on the browser.
  • display_errors = Off:  Errors are silenced.

The line should look like below:

display_errors = Off

After saving and exiting from the editor, we have to restart the web server service to apply the changes. This step is necessary for the new configuration settings to take effect. Reload the phpinfo code and you can verify the directive should updated.

display_errors

If you want to read more blog post . Just visit Tech Transit .

By Sachin G

I am a professional freelance contributor and founder of tech transit. Love to write and lover of education, culture, and community. I have been using it, setting, supporting, and maintaining it since 2009.Linux rocks! Sachin G Follow me on LinkedIn and x formerly twitter