Use PHP error handling

zhaozj2021-02-16  51

Using PHP Error Handling Using PHP Error Processing

Author: Mattias Nilsson Translator: detrox

An all to common error on the web is broken links. You end up with broken links from other sites whenever you rearrange your site. People bookmark some page they like and come back 3 months later only to find a '404 Not Found', giving them no help whatsoever about where to start looking for the page you originally had on your site. Let's solve this, or atleast be friendly to your users and help them get back on track whenever they hit 'a 404'. you can even create a CommON page to report all errors you might encounter while rendering your page.

The most common mistake on the Internet is a bad link. When you reserve your site, you will end it in a bad link from other sites. Some people marked their favorite pages, while only one "404 Not Found" when they went back, no matter how people who want to find the original web page, some. Let us solve him, or treat your users or help them return a web page when they meet a "404". You can even create a public page when you give your web page to report all the errors you might encounter.

PHP TOGETHER with APACHE GIVES You Quite alot of freedom to create your OWN ERROR PAGES, But Requires Some Reconfiguring and a tiny bit of coding. Let's start off with the configuration part.

PHP and Apache gave you a lot of freedom to create your own error handling page, but require some configuration and a small code. Let's start with the configuration first.

The Apache ErrorDocument directive specifies what document (or URI) Apache should redirect a user to in case of an error. It allows you to specify one resource for each and every error code one of your users might run into. Start off by adding a

Apache 's ErrorDocument indicator specifies a such document (or URI), and Apache redirects a user to it when an error occurs. It allows you to develop a resource to handle any error code that your user may encounter. First, add a server configuration to your server.

ErrorDocument 404 / Error.php

directive to your server configuration. This will redirect users that ask for a page that does not exist to the 'error.php' page you will soon write. Do not forget to restart Apache for the changes to take effect. indicator. When the user finds the webpage does not exist, it will boot your users to a web 'error.php' you will have to write. In order to make the change, don't forget to follow the Apache server.

Next, We Write Up A Very Simple Error.php:

Below, we describe in detail a simple error.php

The file you removested () Does Not Exist on this Server.

Please look for the page you wanded at The Front Page

Now Try To Access A Page That Doesn't Exist on Your Server, And Voila, You're At The Error.php Page With A Nice and Friendly Message and a Link to your Front Page!

Now try to read the webpage that does not exist on a server, then you come to Error.php, there is a beautiful and friendly prompt information and a connection to your home page.

Let's extend this. As you can see, I used the REDIRECT_URL variable on the error.php page. This is a variable that Apache sets whenever it invokes an ErrorDocument directive, and gives you a possibility to find the originating resource whenever there's an error. Apache also sets a number of other variables in this case, all of them documented here. Use theese variables to create a nice error page that gives your users a nice and friendly error page instead of the good ol 'boring Apache default page.

Let me expand it again. Just as you see, I use the Redirect_URL variable in Error.php. This variable is set when Apache calls an errordocument indicator, and whenever an error occurs, you may find the resource that triggers it. For this situation, Apache also sets a series of other variables, they are mentioned here. Use these variables to create a perfect error handling page to give your user a beautiful and friendly error handle, replacing those boring Apache default pages.

Throwing errors from a PHP page is quite the same as emulating Apache's behaviour for ErrorDocument directives, you simply redirect the user using a query-string that specifies variables that Apache usually sets as environment variables. This makes it possible to use the same error page for All Kinds of Errors. An Example: Throw errors in the PHP page You can fully follow the behavior of the Apache's ErrorDocument indicator, you can use a query string that makes a variable to simply redirect users, which is usually Apache as the environment. Variables set. This makes it possible to use an error handling page to process all errors, for example:

?

?>

sing the PHP4 feature called output buffering also helps creating generic error reporting functionality. By not flushing the output buffer until you are sure the whole page has rendered error-free, you are able to make Header calls anywhere in your code to redirect the user.

Look at the features called the output buffer also give us a general error report to bring help. Because until you determine that the entire web page is processed without a little error, you can output this buffer, you can use the Header call to redirect users anywhere.

I'll Leave Up to The Reader To Design and Implement His / Her Own Error.php Page To Suit HIS / HER Site. Don't Forget That You Can Include A Form With email submission Possibilities for the users to send you Comments .. I will leave the reader to design and implement the Error.php page of their own suites. Don't forget, you can include a form with email sent, which makes your users provide you with your opinion.

转载请注明原文地址:https://www.9cbs.com/read-25291.html

New Post(0)