如果页面中有错误,请重定向到自定义错误页面 [英] Redirect to custom error page if there is error in the page
问题描述
我是PHP
网站开发的新手.我想知道如果页面上有错误,PHP中是否有任何代码可以将我重定向到页面(将其命名为"myerrorpage.php" )?
I am new to PHP
web development. I want to know is there any code in PHP that redirects me to a page(let's name it "myerrorpage.php") if there is some error on the page?
在JSP
中,可以使用以下代码
In JSP
it's possible using the following code
<%@ page errorPage="myerrorpage.jsp" %>
我想知道PHP中是否有上述任何JSP
类型的代码?是,那么请帮忙
I want to know is there any above JSP
type of code in PHP?is yes then please help
感谢您的帮助...
推荐答案
在php中,当代码中存在错误时,您不会重定向,您只需捕获该错误,然后采取您认为必要的任何操作即可.
In php you don't redirect when there is an error in the code, you simply catch that error and then you take whatever actions you consider necessary.
In order to catch the errors in the code, you must define a custom error handler, using set_error_handler.
您还可以使用 set_exception_handler .
当PHP代码出现错误时,以下代码将重定向到yourerrorpage.php
.
The following code will redirect to yourerrorpage.php
when there is an error on the PHP code.
<?php
function error_found(){
header("Location: yourerrorpage.php");
}
set_error_handler('error_found');
?>
请注意,header("Location: page.php");
重定向类型在内容输出开始后不起作用.
Please note that the header("Location: page.php");
redirect type doesn't work after the content output begins.
或者,您可能要尝试 Apache自定义错误响应
这篇关于如果页面中有错误,请重定向到自定义错误页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!