PC Services


Some PHP Tips and 'Tricks'

Thinking beyond each page and DESIGNING your web site

Tel: 0118 946 3634

linked-in profile facebook profile

   The Company 
   Resources / Examples 
     Miscellaneous Snippets 

Miscellaneous PHP Code Snippets and Thoughts

Page Redirection Errors

Many people use the header( "Location: some-url" ) method of page redirection and rightly so, to do common error reporting or forcing other pages or method of page display for what is currently happening. However the usual problem is that often different servers are setup differently especially for what PHP error levels are displayed, and getting silent failures or just a blank page is not helpful to locate the problem. The problem is usually in your code but without an error or warning message it is difficult to track down. Especially as header() function errors are mainly a blank line being sent before the header() function call, so no error does not help you when you have more than a few include files to go through.

I have found that often E_WARNING is not set in the error display level settings, so I now do this construct for a header() function call to ensure these messages get displayed and I can catch the errors.

$temp = error_reporting( ); error_reporting( $temp | E_WARNING ); ini_set( 'display_errors', true ); header( "Location: somefile.php" ); exit;

Include file not being used

In some server setups, I have a specific include file I want included and for some reason the search path for include files gives up looking for it. Sometimes yes, there is a typo in the name, but often I find PHP gives up before local directory searches due to some server setting. So best thing I have found is use this method to force local directory search by placing './' in front of the file name to force this directory search. Example below

Instead of - include 'file.inc'; Use include './file.inc';

© 2010 onwards by PC Services, Reading UK Last Updated: 18th July 2013
If you encounter problems with this page please email your comments to webmaster