Identifying the errors are very important when learning a new computer language or coding a program. Languages like C and C++ have compilers which display the error message and the lines which contain them only to the programmer (thats you). But with PHP when you’re running a live website and commit any errors in the programming it’ll be displayed to the entire world. Now there is nothing to feel ashamed because everyone commits errors (to error is human, right) the bad thing is that these errors reveal a treasure of information that a potential hacker can use against you and your website. This article will guide you through disabling PHP from displaying errors.
Method 1:- Editing the php.ini file
If you’re hosting your website(s) on a dedicated server/VPS you will have access to the php.ini file. Before editing it please take a copy of it so that it can be restored if something goes wrong. Note that by default the display_errors flag is set to Off state only. If for some reason its On you should modify it. Open the php.ini file using a text editor navigate to the following line
display_errors = On
Replace On with Off, save the file and restart the Web Server. If the web server refuses to start check for syntax errors.
Method 2:- Create/Edit the .htaccess file
This method is for the people using shared hosting services. Some hosting providers think that enabling the server to display PHP errors will help programmers to debug their PHP programs easily. But such good intentions might end up with bad consequences. Create a file named .htaccess (note the dot at the beginning) inside the www or public_html or htdocs folder, if its already there open it using a text editor and add the following line
php_flag display_errors Off
Save the .htaccess file. If you get a “500 Page not Found” error check the syntax used in htaccess.
Testing it
Now its time to test whether this change works. Create a PHP file e.g. file.php and enter a code with errors. Use the following example
<?php print "Missing double quotes and semicolon ?>
Now access the file by typing the url in a web browser (e.g. https://websistent.com/file.php). You should see a blank page now. So you’ve successfully turned off the display_errors option of PHP.
[…] To prevent PHP from displaying such errors at runtime read Disabling PHP Display Errors […]