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

风格不统一 提交于 2019-11-30 19:41:05

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

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

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.

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
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!