Reputation: 297
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...
Upvotes: 8
Views: 25908
Reputation: 1204
It's simple..
You just need to enable by configuring httpd.conf file which is located in xampp/apache/conf/httpd.conf
The next thing you need to do is create a file named '.htaccess' on you root directory.
.
ErrorDocument 404 YourDirectory/error page
eg.
ErrorDocument 404 http://localhost/project/error.php
Upvotes: 0
Reputation: 779
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
Upvotes: 1
Reputation: 425
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
Upvotes: 11
Reputation: 433
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.
Upvotes: 0