PHP Silence Warnings Code Examples

For PHP sites: How to silence warnings:

One failing with PHP is that when warnings are displayed to the browser window, information might be provided that could aid someone in hacking into your site. It could include filenames, paths, database name or database table information.

The best way to handle this is to set your PHP.ini file to hide errors and log them to your web server. You can always view all errors in the web servers error logs but no site visitor will see them accidentally.

display_errors = Off
log_errors = On


But what if your host doesn't give you access to the php.ini file? Fortunately it's easy to mask any line of code that might produce a warning such as a MySQL Query. Simply prepend the line of code with an "@" symbol:

$TResult = @mysql_query($sQuery, $hDB);


This will hide any errors that a broken query string might produce. The error is still viewable in the error log and a site visitor has not been shown any confidential information about your database and/or tables.





P.O. Box 67
Bellingham, MA
02019

        
28 Summer St
Medway, MA
02053