PHP Tips and Tricks
From SuperbHosting Support Wiki
This article is meant to outline some handy knowledge for dealing with various and sundry PHP-related issues.
Contents |
Overriding PHP.ini Settings
Often times a Shared Hosting plan will have some sort of forum, bulletin board, or other PHP web application that requires specific changes to the PHP configuration variables. This PHP.ini directives list gives a comprehensive view of all available PHP configuration directives, and whether or not they can be overridden in a user's local directory. If the directive one wants to override is listed as PHP_INI_ALL or PHP_INI_PERDIR, see below on how to perform the override.
Unix
Unix systems are particularly simple:
- Create a .htaccess file in the directory the customer's web app will live in
- Add the directives just as you would in the PHP.ini file, prepended with one of the following:
- php_value for non-boolean values (Example: php_value log_error_max_len 1024)
- php_flag for boolean values (Example: php_flag log_error on)
- Save the .htaccess file
Windows
Windows is a bit more difficult due to eccentricities in IIS:
- Create a php.ini file in the user's public_html directory
- Add the directives just as you would in the standard PHP.ini file. No need to prepend anything here.
- Save the php.ini file
- Open the IIS Manager, select Application Pools
- Right click on Default and select Recycle. Do the same for PHP.
For both methods, make a quick phpinfo() file to check the local vs. global values of the configuration variables you're changing to make sure everything has gone through.
Troubleshooting PHP Scripts
You can insert the following code block to log all PHP errors to a file:
ini_set('error_reporting', E_ALL);
error_reporting(E_ALL | E_STRICT);
error_reporting(E_ALL);
ini_set('log_errors',TRUE);
ini_set('html_errors',FALSE);
ini_set('error_log','/tmp/log_name');
ini_set('display_errors',FALSE);
Be sure to replace log_name with the name of the file you want to use, and possibly place the logs somewhere else than /tmp if you want to store them long-term. Running a tail -f log_name while refreshing the PHP page in a browser will reveal any errors.
