View Single Post
Old 03-22-2004, 01:15 PM  
Seb From Holland
Confirmed User
 
Join Date: May 2002
Location: The Netherlands
Posts: 2,747
GFY Tutorial: Error Pages In PHP!

Hey GFY,

Here's a little contribution from me to the board. I know many of you use errorpages on your servers and now that the web has become one dynamic place, it's a smart thing to update those static old pages to nice dynamic ones. The example I'm going to show you is really simple and anyone who has little knowledge of HTML/PHP can do it.

Most of the times, your .htaccess file which contains links to the error documents on the server looks like this:

ErrorDocument 400 http://www.domain.com/errors/400.html
ErrorDocument 401 http://www.domain.com/errors/401.html
ErrorDocument 403 http://www.domain.com/errors/403.html
ErrorDocument 404 http://www.domain.com/errors/404.html
ErrorDocument 405 http://www.domain.com/errors/405.html
ErrorDocument 410 http://www.domain.com/errors/410.html
ErrorDocument 500 http://www.domain.com/errors/500.html

Which is perfect in it's way, apart from the fact that they are links to several static pages.

Let's say we want to have just one error page covering all errors (there are somewhat 22 client and server errors to my knowledge). How do you do it? Simple:


1.) Create a new .php document, say error.php or any other name if you like.

2.) Make an if-statement or a case-statement in which you define all errors. So if the error.php?id=404, the if statement should be able to recognize it and add the value "page not found" to the doc.ument. Here's a little if-statement I made with different errors:

<center></center>

3.) Place this bit of code on top of all your HTML, even before the <html> opening.

4.) Create a page of your preference. Perhaps you want the error page in the same style as your Website, thats up to you. In this example I made a very simple page, just displaying the error. Of course, you can do with it whatever you like. Here's my HTML setup:

<center></center>

Don't mind what's in the title tag. That's not part of the error code.

So now you have created a page that contains every error. You can throw away you 15 old static errorpages. No use for them anymore. The only thing you have to do now is to adjust your .htaccess file. Make it look like this:

ErrorDocument 400 http://www.domain.com/error.php?id=400
ErrorDocument 401 http://www.domain.com/error.php?id=401
ErrorDocument 403 http://www.domain.com/error.php?id=403
ErrorDocument 404 http://www.domain.com/error.php?id=404
ErrorDocument 405 http://www.domain.com/error.php?id=405
ErrorDocument 410 http://www.domain.com/error.php?id=410
ErrorDocument 500 http://www.domain.com/error.php?id=500

Download the codes here:
http://www.signaturetracker.com/gfy/error.txt (error.php code)
http://www.signaturetracker.com/gfy/htaccess.txt (.htacces code)

Enjoy!

Seb.

(Don't forget to upload your new or adjusted .htaccess in ASCII format.)
Seb From Holland is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote