Skip to content
druu edited this page Dec 23, 2012 · 9 revisions

These are a number of different example on how you can setup and run PHP Error. They aren't the only way, but you can just copy and paste if you want to get going quickly.

The current version of PHP-Error can be found here.

In all cases, 'display_errors' must be set to 'On' for PHP Error to work. This is a safety feature.

Run in a single project

  • Place php_error.php into your project
  • At the entry point of your site add ...
    require('php_error.php');
    \php_error\reportErrors();

I want it outside my project, but running on all dev sites

  • Place php_error.php somewhere on your PC, for example: C:\php\php_error.php
  • open your php.ini file
  • find 'auto_prepend_file'
  • set it to auto prepend php_error.php
  • add the option 'php_error.autorun = On'
    auto_prepend_file = "C:\php\php_error.php"
    php_error.autorun = On

It will need permissions!

Some web servers, but not all, need read and execute access to the php_error.php file for this setup to work. For example IIS requires this.

This can be done on Windows by adding 'Everyone' to the security list of users, with read and execute permissions set.

On linux you can achieve this by opening a terminal, moving to the folder where php_error.php is located, and running:

    chmod a+rx ./php_error.php

I want it outside my project, but only run on selected projects

  • Place php_error.php somewhere on your PC, for example: C:\php\php_error.php
  • open your php.ini file
  • find 'auto_prepend_file'
  • add 'php_error.php' to this setting
    auto_prepend_file = "C:\php\php_error.php"
  • find the entry point to your project
  • run 'reportErrors' if php_error is loaded (so in production this silently fails)
    if ( function_exists('\php_error\reportErrors') ) {
        \php_error\reportErrors();
    }

Again, the server may need read and execute permissions, like in the previous setup.

Disable it globally in production (just in case it gets deployed)

But you can forcefully shut it down globally by:

  • open php.ini on your production server
  • add the option 'php_error.force_disabled = On'
    php_error.force_disabled = On

Alternatively:

    display_errors = Off

Either of these will cause PHP Error to silently do nothing.

WordPress

Usage with WordPress is the same as with any other project.

I want to use it directly on WordPress

  • open the 'functions.php' file in your WordPress theme
  • require php_error
  • set to reportErrors
    require( 'php_error.php' );
    \php_error\reportErrors();

and to use it automatically from php.ini?

  • Place php_error.php somewhere on your PC, for example: C:\php\php_error.php
  • open your php.ini file
  • find 'auto_prepend_file'
  • set it to auto prepend php_error.php
  • add the option 'php_error.autorun = On'
    auto_prepend_file = "C:\php\php_error.php"
    php_error.autorun = On

The 'wordpress' option

On older versions of WordPress, there were many minor errors in the core, which are caught by PHP Error when error level is raised to maximum.

These days this is not an issue, however if needed, you can use the 'wordpress' option to automatically lower the error level to one which is less strict for WordPress.

If used in the header:

    \php_error\reportErrors(array(
        'wordpress' => true
    ) );

If used from php.ini, add the line:

php_error.wordpress = On

However the ideal solution is to upgrade your copy of WordPress!

Code Igniter

PHP Error cannot take over 100% of CI's errors, at least not out of the box, but can still be used for general PHP errors, such as for pointing out undefined variables. Here are the steps to take:

I want it inside the project

  • place php_error.php in your application folder.
  • then open the autoloader settings, at application/config/autoload.php
  • add the code to load and run PHP Error at the top
    require( __DIR__ . '/../php_error.php' );
    \php_error\reportErrors( array(
            'application_folders' => 'application',
            'ignore_folders' => 'system'
    ) );

I want it outside the project

  • download php_error and place it somewhere on your machine, such as at C:\php\php_error.php
  • then open up your php.ini file
  • find the 'auto_prepend_file' setting
  • add 'php_error.php' to this setting
    auto_prepend_file = "C:\php\php_error.php"
  • now open your Code Igniter autload configuration, at your_project/application/config/autoload.php
  • add at the top of this file
    if ( function_exists('\php_error\reportErrors') ) {
        \php_error\reportErrors( array(
                'application_folders' => 'application',
                'ignore_folders' => 'system'
        ) );
    }

The 'application' and 'ignore' folders are optional, but allows PHP Error to tell the difference between application code, and CI code.

I want it to handle ALL Errors

As mentioned above PHP Error cannot take over 100% of CI's errors, but there is a workaround to enable it:

  • enable CodeIgniter's System Hooks in /application/config/config.php by setting $config['enable_hooks'] = TRUE;
  • create the hook in /application/hooks (e.g. phpe.php) and (re)create the error handlers.
  • do NOT forget to set the 'clear_all_buffers' option. Otherwise you'll end up with a blank screen.
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class PHPE {
	static function reload() {
		\php_error\reportErrors(array(
			'clear_all_buffers' => true
			)
		);
	}
}
  • register the hook in /application/config/hooks.php
$hook['pre_controller'] = array(
                                'class'    => 'PHPE',
                                'function' => 'reload',
                                'filename' => 'phpe.php',
                                'filepath' => 'hooks'
                                );

This hook overrides CI's error handling, allowing PHP Error to handle every error, while clearing every output buffer CI creates.