Using PHP for error handling

Source: Internet
Author: User
Tags error code error handling header variables php and resource variable
Error | One of the common errors in error handling on the Web is an invalid link. Once an invalid link appears from another site, you will reschedule your


's site. People will save their favorite sites in a book, and if they visit again after three months, only the ' 404 Not Fount '


will not give them task help, tell them how to find the original information from your site. Let's solve this problem, or at least


gives your users a friendly help and can get some traces if they encounter a ' a 404 ' error. You can create an ordinary page to


reports all of the errors encountered while processing your page. PHP and Apache are free to let you create your own error pages, but you need to reconfigure them and make a small number of generations of


code. Let's learn the configuration section first.





Apache's errordocument instructions are used to indicate which document (or URI) Apache should redirect to when an error occurs. It allows you to


specify a resource for each error code that your users may encounter. By adding a errordocument 404 to Your server configuration


/error.php instructions. This will redirect to ' error.php ' when the user accesses a nonexistent page, and we'll write the


' error.php ' page. Don't forget to restart Apache for the changes to take effect.





Then, we write a simple error.php:











the file you requested (<?= $REDIRECT _url?>) does not exist on this server.


Please find the file you want from <a href= "/" > front page </A>.





now try to read a page that doesn't exist on your server, you can see error.php, it has a good and


friendly message, and there is also a link to the previous page.





let's expand it. As you can see, I used the Redirect_url variable in the error.php. This variable is Apache in


executes a errordocument indication, and gives a possibility to find the original resource. In this case, Apache


also sets a number of other variables, all of which can be found here. Using these variables may create a good error page for


User A nice and friendly error page, instead of the default page given by Apache.





output error from PHP page


from a PHP page output error and analog Apache to errordocument instructions to do much like, you simply redirect the user,


by using the query-string variable, and Apache is usually set in the environment variable. This allows you to use the same error page to place the


all kinds of mistakes. Here is an example:





----------------------------------------------------------------------


<?php





function Throw_error ($message) {


$error _page = "/err/error.php";





$error _url = $error _page;


$error _url. = "? redirect_error_notes= $message ";


$error _url. = "&redirect_url=". $GLOBALS ["Php_self"];


$error _url. = "&redirect_request_method= $REQUEST _method";


$error _url. = "&redirect_status=501";


Header ("status:501");


Header ("Location: $error _url");


exit;


}





Ob_start ();


//Use output buffers to output errors anywhere on this page





if (!condition) {


Throw_error ("The condition Failed");


}





Ob_end_flush ();


//page processing complete, refresh output buffer





?>------------------------------------------------------------------


using the PHP4 output buffering feature can also be useful for generating generic error reporting. But after you confirm the entire error page,

When
, do not forget to refresh the buffer, you can use the header to redirect in your code wherever possible.





readers can design, implement their own error page to adapt to his/her site. Don't forget, you can include a
with an email.

the form on the error page so that the user can give you some feedback.





Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.