Redirect to custom error page if there is error in the page
Asked Answered
U

4

8

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

Unchain answered 31/5, 2014 at 13:36 Comment(0)
B
11

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

Bobbi answered 31/5, 2014 at 13:48 Comment(0)
B
1

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

Blowup answered 31/5, 2014 at 13:52 Comment(1)
its also a good one but i got my ans which is a PHP code.. thanks for contribution +1 :)Unchain
S
0

You can always send a new header with the new location like:

header('Location: myerrorpage.php');

Just make sure you don't output anything before calling the header() because then it will not work. Then you ofcourse just need to check if an error occured and then call the header() appropriately.

Seton answered 31/5, 2014 at 13:47 Comment(1)
this code just redirect me to some page.but how someone can check is there any error in the page?Unchain
W
0

It's simple..
You just need to enable by configuring httpd.conf file which is located in xampp/apache/conf/httpd.conf

  1. Search for <Directory "C:/xampp/htdocs">.
  2. Under the section, look for #AllowOverride All
  3. Just Remove the '#' which means comment and you're ready for the further step.

The next thing you need to do is create a file named '.htaccess' on you root directory.

  1. Make sure it is just .htaccess
  2. Edit using your favorite text editor(Sublime, Atom, Notepad++, etc)
  3. Write the code....

.

ErrorDocument 404 YourDirectory/error page

eg.

ErrorDocument 404 http://localhost/project/error.php
Wuhsien answered 9/7, 2017 at 13:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.