By Andrew Spiers
Apache Web Server enables you to deliver custom error messages, instead of the standard error one. This means instead of displaying the '404 - Page Not Found' message, you could display something more informative, or if you are that way inclined, funny. In this instance, it will allow us to setup different error pages for WML and HTML browsers.
So what we are going to do here is to create a custom error handler script in PHP. The script will log the error in a MySql table, will detect if the user is using a WML or HTML browser and then display an appropriate error message.
First thing to do is to setup Apache to call our error handling script when an error occurs. This is done by using the 'ErrorDocument' directive. The format of the directive is :
ErrorDocument error-code error-routine
So if you want to run a script called errorhandler.php3 whenever there is a page not found (404) error, then you would have :
ErrorDocument 404 errorhandler.php3
The directive can be placed in one of two places, if you want it to apply for the whole web server, then it needs to be placed in httpd.conf. If you want it to apply it on a per-directory basis then it needs to be placed in a access control file for that directory (.htaccess). Consult your server administrator if you need help.
Next thing to do is to setup our MySql table, that will log all of the errors. The schema is :
CREATE TABLE error_log (
| id |
int(11) NOT NULL auto_increment, |
| date |
char(10), |
| time |
char(5), |
| page_type |
char(4), |
| page_requested |
char(50), |
| error_type |
int(4), |
| PRIMARY KEY |
(id), |
| KEY id |
(id) ); |
As you can see we will be logging the date/time of the error, whether the request was wml / html, what page was requested and the type of error.
Next we have the script :
1 <?
2 /* MySQL Configuration Section */
3 $hostname = "Localhost:3306";
4 $userid = "userid";
5 $password = "password";
6
7 if(strstr(strtolower($HTTP_ACCEPT), "text/vnd.wap.wml")) { $page_type = "wml"; } else { $page_type = "html"; }
8
9 $date = date("Y/m/d");
10 $time = date("H:i");
11 $page = getenv("REDIRECT_URL");
12 $error_type = getenv("REDIRECT_STATUS");
13 $db = mysql_connect($hostname,$userid,$password) or die (mysql_error());
14 mysql_select_db("ranelagh",$db) or die (mysql_error());
15 $result = mysql_query("INSERT INTO error_log (date, time, page_type, page_requested, error_type) VALUES ('$date','$time','$page_type','$page','$error_type')") or die(mysql_error());
16
17 switch ($page_type) {
18 case "wml":
19 display_wml_error($error_type);
20 break;
21 case "html":
22 display_html_error($error_type);
23 break; }
24
25 function display_wml_error($error_type) {
26 header("Content-type: text/vnd.wap.wml");
27 echo("<?xml version=\"1.0\"?>\n");
28 echo("<!DOCTYPE wml PUBLIC \"-//WAPFORUM//DTD WML 1.1//EN\" \"http://www.wapforum/DTD/wml_1.1.xml\">\n\n");
29 echo "<wml><card id=\"Error\">";
30 echo "<p>I am sorry but there has been an $error_type error.</p>";
31 echo "</card></wml>"; }
32
33 function display_html_error($error_type) {
34 echo "<html><title>Error</title><body>";
35 echo "<p>I am sorry but there has been an $error_type error.</p>";
36 echo "</body></html>"; }
37 ?>
Lets look at the script in detail:
Lines 3-5 is the configuration section for access to the MySql database, change these lines to reflect your MySql setup. Line 7 checks the environmental variable HTTP_ACCEPT is see if the browsers accepts "text/vnd.wap.wml" as a valid MIME type, if is does then it is a WML browser, if not then it is a HTML browser. Page_type is set to reflect this.
Lines 9-12 setup the value of the fields that will be inserted into our error_log table. Line 9 and 10 setup the date and time. Line 11 gets the name of the requested page and line 12 gets the error code.
Lines 13-15 connects to MySQL, select the required database and then inserts a row into our error_log table.
Lines 17-23 checks to see if the $page_type has been set for wml or html and branches to the corresponding function passing the error_type into that function. Lines 25-31 display a wml error page and lines 33-36 display a html error page. Both pages display a message along with the error message number.
So as you can see, a very simple script to log a 404 error into a table and then display either a wml or html error page. This script could be expanded to cover other types of error by setting up the appropriate ErrorDocument directives. You could also change the script to handle each error type differently by checking the value of $error_type and then branching to a different function for each error type. The possibilities are endless !!!
|