set_execption_handler
set_exception_handler ( callable $exception_handler )
设置默认的异常处理程序,用于没有用 try/catch 块来捕获的异常。 在exception_handler 调用后异常会中止。
代码如下:
<?php
header('content-type:text/html;charset=utf-8');
class ExceptionRedirectHandler{
protected $_exception;
public $redirect = '404/index.html';//自己网上下载的404页面路径
public $file = 'E:\php39\impover\redirect.log';//保存的日志的位置
private $_msg='';
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');//307重定向
header('Cache-Control:no-cache,must-revalidate');//不缓存
header('refresh:1;url='.$self->redirect);
exit();
}
public function log(){
//写入日志
$this->_msg =<<<EOF
异常信息为:{$this->_exception->getMessage()}
异常文件为:{$this->_exception->getFile()}
异常行号为:{$this->_exception->getLine()}
EOF;
error_log($this->_msg,3,$this->file);
}
}
set_exception_handler(array('ExceptionRedirectHandler','handle'));
$link = new pdo('mysql:host=localhost;dbname=t_test','root','cuowu');
if (!$link) {
throw new Exception('数据库连接失败,请查询原因');
}
?>
得到的结果是数据库连接错误,跳转到了如下页面:
附: PHP中Header使用的HTTP协议(如需常用,可以把它封装成一个函数,放数组中)以及常用的header头设置:
100 => "HTTP/1.1 100 Continue",
101 => "HTTP/1.1 101 Switching Protocols",
200 => "HTTP/1.1 200 OK", ///200 正常状态
201 => "HTTP/1.1 201 Created",
202 => "HTTP/1.1 202 Accepted",
203 => "HTTP/1.1 203 Non-Authoritative Information",
204 => "HTTP/1.1 204 No Content",
205 => "HTTP/1.1 205 Reset Content",
206 => "HTTP/1.1 206 Partial Content",
300 => "HTTP/1.1 300 Multiple Choices",
301 => "HTTP/1.1 301 Moved Permanently",
302 => "HTTP/1.1 302 Found",
303 => "HTTP/1.1 303 See Other",
304 => "HTTP/1.1 304 Not Modified",
305 => "HTTP/1.1 305 Use Proxy",
307 => "HTTP/1.1 307 Temporary Redirect",
400 => "HTTP/1.1 400 Bad Request",
401 => "HTTP/1.1 401 Unauthorized",
402 => "HTTP/1.1 402 Payment Required",
403 => "HTTP/1.1 403 Forbidden",
404 => "HTTP/1.1 404 Not Found",
405 => "HTTP/1.1 405 Method Not Allowed",
406 => "HTTP/1.1 406 Not Acceptable",
407 => "HTTP/1.1 407 Proxy Authentication Required",
408 => "HTTP/1.1 408 Request Time-out",
409 => "HTTP/1.1 409 Conflict",
410 => "HTTP/1.1 410 Gone",
411 => "HTTP/1.1 411 Length Required",
412 => "HTTP/1.1 412 Precondition Failed",
413 => "HTTP/1.1 413 Request Entity Too Large",
414 => "HTTP/1.1 414 Request-URI Too Large",
415 => "HTTP/1.1 415 Unsupported Media Type",
416 => "HTTP/1.1 416 Requested range not satisfiable",
417 => "HTTP/1.1 417 Expectation Failed",
500 => "HTTP/1.1 500 Internal Server Error",
501 => "HTTP/1.1 501 Not Implemented",
502 => "HTTP/1.1 502 Bad Gateway",
503 => "HTTP/1.1 503 Service Unavailable",
504 => "HTTP/1.1 504 Gateway Time-out"
header('Content-language: en'); //设置上下文语言
// 设置页面最后修改时间(多用于防缓存)
$time = time() - 60; //建议使用filetime函数来设置页面缓存时间
header('Last-Modified: '.gmdate('D, d M Y H:i:s', $time).' GMT');
// 设置头文件类型,可以用于流文件或者文件下载 header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="example.zip"');
header('Content-Transfer-Encoding: binary');
readfile('example.zip');//读取文件到客户端
//禁用页面缓存 header('Cache-Control: no-cache, no-store, max-age=0, must-revalidate');
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Pragma: no-cache');
//设置页面头信息 header('Content-Type: text/html; charset=iso-8859-1');
header('Content-Type: text/html; charset=utf-8');
header('Content-Type: text/plain');
header('Content-Type: image/jpeg');
header('Content-Type: application/zip');
header('Content-Type: application/pdf');
header('Content-Type: audio/mpeg');
header('Content-Type: application/x-shockwave-flash');
//....至于Content-Type 的值 可以去查查 w3c 的文档库,那里很丰富