PHP中的错误处理并重定向到错误页面

I would like to log an error if occured on a php webpage and redirect the user to a custom error page. How can this be acheived. I am currently using the

set_error_handler('my_error_handler');

for logging all errors.

What is the best practice to show the error page after handling the error in php.

If you like, you can use PHP Exceptions. They're not too popular (currently, anyway) but they work just swell.

Just throw new MyException("Error text") and catch it during your main page's execution, where you can print your pretty error page.

Redirect should never be used.
Just show error page in place. Just add these lines right in your error handler

header("HTTP/1.1 503 Service Unavailable");
readfile($_SERVER['DOCUMENT_ROOT']."/503.html");
exit;

of course it would work only if your application is properly planned using templates, doing no output before all logic is done

I wrote a custom class for this:

/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

class CommonException extends Exception {
    private $logFile = "../log/commonEx.log";
    private $log = Array();
    public $code = "";

    public function __construct($message = null, $code = 0) {                             
        $exLog['msg'] = $message;
        $exLog['code'] = $code;
        $this->code = $code;

        $exLog['file'] = $this->getFile();
        $exLog['line'] = $this->getLine();

        $this->log = $exLog;
        $this->_writeToLog($exLog);
        sfLoader::loadHelpers('I18N');
    }

    public function display() {
        return $this->log;
    }

    private function _writeToLog($exLog) {
        error_log(implode("|", $exLog) . "
", 3, $this->logFile);
    }
}

In your try .. catch block:

try {
    // ...
} catch (Exception $e) {
    $errMsg = new CommonException($e->getMessage(), $e->getCode());
    // redirect to anywhere you want
}