简体   繁体   English

如果页面中有未处理的错误,如何在php中重定向?

[英]How do I redirect in php if there's an unhandled error in the page?

In asp.net I can easily redirect the user to a customer error page if there's an unhandled error in the page by putting this on the web.config: 在asp.net中,如果页面中存在未处理的错误,我可以轻松地将用户重定向到客户错误页面,方法是将其放在web.config中:

<customErrors mode="RemoteOnly" defaultRedirect="Oops.aspx"  />

I studying php right now and what I want to happen is redirect the user to another page if there's an unhandled error. 我现在正在研究php,如果发生未处理的错误,我想将用户重定向到另一页。

You don't want to redirect if there is an error. 如果发生错误,您不想重定向。 You want to respond with http 500. 您想用http 500回复。

For non-fatal errors, you can use php's set_error_handler and set_exception_handler functions. 对于非致命错误,可以使用php的set_error_handlerset_exception_handler函数。 For fatal errors, PHP will respond with 500 and a blank page, but you can configure the web server to serve a custom page. 对于致命错误,PHP将以500和空白页作为响应,但是您可以将Web服务器配置为提供自定义页面。 For Apache for example, you put this line in the config: 例如,对于Apache,可将以下行放入配置中:

ErrorDocument 500 /error500.html

I would suggest maybe thinking of using Exceptions and or creating an Error handling Class dependent on the errors your application is producing. 我建议也许考虑使用异常或根据应用程序产生的错误创建一个错误处理类。

As to redirecting within PHP, you can you use the header() method in PHP. 至于在PHP中进行重定向,您可以在PHP中使用header()方法。

Header function 标头功能

The simplest way you can use this method is to do the following: 使用此方法的最简单方法是执行以下操作:

header('Location: www.example.com');

The above snippet of code will redirect to the specified site in the code. 上面的代码片段将重定向到代码中的指定站点。

The header() method will allow you to redirect to another page or site. header()方法将允许您重定向到另一个页面或站点。

More Information about Exceptions in PHP if you're interested. 如果您有兴趣,请参阅有关PHP异常的更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM