WeberDev.com PHP and MySQL Code

LOG IN
BEGINNER GUIDES  |  PHP CLASSES  |  CODE SEARCH  |  ARTICLES SEARCH  |  PHP FORUMS  |  PHP MANUAL  |  PHP FUNCTIONS LIST  |  WEB SITE TEMPLATES
Start typing to search for PHP and MySQL Code Snippets and Articles Search
Submit a code Example / Snippet Submit Your Code
Search Engine Optimization Monitor SEO Monitor
Web Site UpTime Monitor UpTime Monitor
WeberDev's Monthly code contest PHP Code Contest
Your Personal Examples List My Favorite Examples
Your Personal Articles List My Favorite Articles
Edit Account Info Update Your Profile
PHP Code Search
Web Development Forums
Learn MySQL Playing Trivia
PHPBB2 Templates
Web Development Index
Web Development Resources
Web Development Content
PHPClasses
PHP Editor
PHP Jobs
Vision.To Design
Ajax Tutorials
PHP Programming Help
PHP/MySQL Programming
Webmaster Resources
Webmaster Forum
XML meta language
website builder
Mobile Dev World

Go Back Add a Comment Send this example to a friend Add this Article to your personal favoritest for easy future access to your favorite Code Examples and Articles. Submit a code example Print this code example.
BACK ADD A COMMENT SEND TO A FRIEND ADD TO MY FAVORITES ADD CODE EXAMPLES PRINT
Title : Logging 404 errors in your custom statistics using Apache and a PHP script.
Categories : Apache, Web Servers, PHP, Errors and Logging Click here to Update Your Picture
Ioannis Cherouvim
Date : May 12th 2005
Grade : 3 of 5 (graded 6 times)
Viewed : 8412
File : No file for this code example.
Images : No Images for this code example.
Search : More code by Ioannis Cherouvim
Action : Grade This Code Example
Tools : My Examples List

  Submit your own code examples 
 

Title :Logging 404 errors in your custom statistics using Apache
and a PHP script.

Author :Ioannis Cherouvim
Email :morales[at]hack[dot]gr
Date :2005-05-12

Story :Are you using you own statistics generator for your php
website traffic ? Yes, the kind of statistics that need an
include statement on the top of each of php files you want
to log, in order to capture the user's visit. Yes I do as
well, it's cool, nice and makes you a better programmer.

Problem :The problem is that these stats are triggered only when
the user actually attempts to access a php that exists in
your server. If he types in yoursite.com/some-garbage-here
this will not be logged in your custom stats. It will only
be found in the regular apache logs, and it will be possible
to see that if you are using statistics that are based on
parsing of the server logs.

Solution:Using a little help from the apache webserver we can capture
404 hits as well. All we need to do is instruct apache to
redirect all not found attempts to a new script we are going
to make which will handle this case. The script will be
called error.php and will reside somewhere inside our
webserver. It will construct an appropriate and informative
error message containing the request method, status, URL and
query string information and log that in our custom stats.
Then it will immediately redirect the user to our real
website. So a) we track 404 errors which might be important
for us to know about internal broken links or maybe hack
attempts and b) never let any user see a 404 error message
by simply redirecting him to the good working URL of our
site, thus never loosing a 'customer'.

Step a) :Put these lines in UNIX type text file called .htaccess in
the root of your website. It will instruct the server to
redirect the user in case of errors 403, 404 and 500.

ErrorDocument 403 /core/error.php
ErrorDocument 404 /core/error.php
ErrorDocument 500 /core/error.php

Step b) :Construct an error.php script in the appropriate directory
in your server (in this example into the /core/ directory).
Construct the desired error string and call the statistics
by logging this error. Then redirect to correct site. This
example will log something like :

ERROR[GET]>404>/ind3ex.php?file=get

Which shows us that there was a 404 error in the users attempt
to call an 'ind3ex.php' file which does not exist in our
server, using a 'GET' method and using a 'file=get' query string.
<?php

    $error
= "ERROR[".
       
$_SERVER[REDIRECT_REQUEST_METHOD].
       
"]>".
       
$_SERVER[REDIRECT_STATUS].
       
">".
       
$_SERVER[REDIRECT_URL].
       
"?".
       
$_SERVER[REDIRECT_QUERY_STRING];

   
//call our stats
   
define("_STATS_ERROR", $error);
   
define("_STATS", "../stats/log.php");
    if (
is_readable(_STATS)) include_once(_STATS);

   
//redirect user to the correct page
   
header("Location: ../index.php");
?>



Caldera Openlinux 1.2 Standard
Categories : Web Servers, MySQL, PHP, Apache, Databases
apache_lookup_uri -- Perform a partial request for the specified URI and return all info about it
Categories : PHP, PHP Functions, Apache, Web Servers
DB Connection Function with error handling and email failure notices
Categories : PHP, MySQL, Errors and Logging, Databases, Errors and Logging
include php3 files
Categories : Filesystem, PHP, Apache, Web Servers
Easy windows installation kit for win 9* PHP4 MySQL Apache phpMyAdmin
Categories : Installation, PHP, MySQL, Apache, Web Servers
PHP Apache and SSL
Categories : PHP, Apache, Web Servers
Why do i get a "Save As" dialog box when I type in the url of a php3 file
Categories : PHP Configuration, PHP, Apache, Web Servers
gd libraries make gmake php3 apache-1.3.0 gd1.3 FreeBSD-2.2
Categories : Installation, PHP, Apache, Web Servers
WebServerSpy checks which kind of Webserver is running, Apache, Netscape, Fasttrack, IIS, HTTP-Header, HTTP 1.0, GET, spy, WWW
Categories : HTTP, Network, Apache, PHP, Web Servers
phpCMS a content-management-system written in php.
Categories : Content Management, Apache, Complete Programs, Web Servers, PHP
How to use your own 404 Error file from .htaccess.
Categories : Apache, Web Servers
How do you specify the default document in Apache?
Categories : Apache, HTTP, Web Servers
PHP4 MYSQL Authentication Script with cookie. Short & Sweet
Categories : Authentication, Apache, Cookies, PHP, MySQL
PHP Based Apache + Mysql Error Log Parser
Categories : PHP, PHP Classes, Apache, MySQL, Log Files
Error mailing logging facility
Categories : PHP, Errors and Logging, Email