Randel Ramirez
Randel Ramirez

Reputation: 3761

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:

<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.

Upvotes: 0

Views: 1273

Answers (2)

Haroon
Haroon

Reputation: 1155

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.

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.

More Information about Exceptions in PHP if you're interested.

Upvotes: 1

troelskn
troelskn

Reputation: 117457

You don't want to redirect if there is an error. You want to respond with http 500.

For non-fatal errors, you can use php's set_error_handler and set_exception_handler functions. For fatal errors, PHP will respond with 500 and a blank page, but you can configure the web server to serve a custom page. For Apache for example, you put this line in the config:

ErrorDocument 500 /error500.html

Upvotes: 3

Related Questions