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>