[PHP错误异常]①⑧--页面重定向

Paste_Image.png

ExceptionRedirectHandler.php

<?php

class ExceptionRedirectHandler
{
    protected $_exception;
    protected $_logFile = 'D:/error/redirectLog.log';
    public $redirect = '404.html';

    public function __construct(Exception $e)
    {
        $this->_exception = $e;
    }

    public static function handle(Exception $e)
    {
        $self = new self($e);
        $self->log();
        while (@ob_end_clean()) ;
        header('HTTP/1.1 307 Temporary Redirect');
        header('Cache-Control:no-cache,must-revalidate');
        header('Expires:Sat,27 Mar 2015 13:28:48 GMT');
        header('Location:' . $self->redirect);
        exit(1);
    }

    public function log()
    {
        error_log($this->_exception->getMessage() . PHP_EOL, 3, $this->_logFile);
    }
}

set_exception_handler(array('ExceptionRedirectHandler', 'handle'));

$link = mysql_connect('localhost', 'sdsds', 'sdsds');
if (!$link) {
    throw new Exception('数据库可能被攻击,抓紧查看情况');
}

404.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h2>404</h2>
</body>
</html>
Paste_Image.png
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容