美文网首页
[PHP错误异常]①⑧--页面重定向

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

作者: 子木同 | 来源:发表于2017-09-15 17:20 被阅读18次
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

相关文章

网友评论

      本文标题:[PHP错误异常]①⑧--页面重定向

      本文链接:https://www.haomeiwen.com/subject/czxesxtx.html