Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redirect to custom error page if there is error in the page

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?

In JSP it's possible using the following code

<%@ page errorPage="myerrorpage.jsp" %>

I want to know is there any above JSP type of code in PHP?is yes then please help

Any help is appreciated...

like image 215
Anonymous Avatar asked May 31 '14 13:05

Anonymous


2 Answers

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.

Also you can handle the uncaught exceptions using set_exception_handler.

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');
?>

Please note that the header("Location: page.php"); redirect type doesn't work after the content output begins.


Alternatively, you might want to try Apache custom error responses

like image 181
Vasile Goian Avatar answered Sep 24 '22 20:09

Vasile Goian


You can handle error in php using .htaccess.Create .htaccess file in root of website and add following into file

ErrorDocument 400 /400.html

ErrorDocument 401 /401.html

ErrorDocument 403 /403.html

ErrorDocument 404 /404.html

ErrorDocument 500 /500.html

ErrorDocument 502 /502.html

ErrorDocument 504 /504.html

Now create all pages 400.html,401.html etc pages into root of your website.When error occurred user will redirect to the pages.

Thanks

like image 20
nikunj gandhi Avatar answered Sep 24 '22 20:09

nikunj gandhi