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
PHP Web Logs (BLogs)
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
Submit Site
Forex Trading Online forex trading platform

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 : Intelligent 404 Handler
Categories : PHP, Errors and Logging Click here to Update Your Picture
Sarah King
Date : Aug 30th 2004
Grade : 3 of 5 (graded 3 times)
Viewed : 11628
File : No file for this code example.
Images : No Images for this code example.
Search : More code by Sarah King
Action : Grade This Code Example
Tools : My Examples List

  Submit your own code examples 
 

Intelligent 404 Handler
track.php
Author: Sarah King
This script detects the type of file that was requested and
outputs accordingly.
A broken image can cause infinite looping if the redirect
page also calls the missing image

This script will email the administrator the first time the
error is encountered.
The filename is then recorded.
Hackers, search engine indexers and link checkers will
request pages which don't exist and therefore you don't
want to be alerted every time someone calls the page,
just the first time.

The Useragent and IP information is provided so that you can
determine if the bad page
was from a broken link, a bot or a hacker. If you suspect
action from a hacker tighten security straight away.

Your server will "throw" different error codes depending on
the problem. You may want to add code to handle each type of
error.

Errors in Client
400 Bad syntax
401 Unauthorized
402 Not Used (Payment Granted)
403 Forbidden
404 Not Found

Errors in Server
500 Internal Error
501 Not Implemented
502 Overloaded
503 Gateway Timeout

To call the scripts add the following to your .htaccess file.

ErrorDocument 404 /track.php



<?php
//choose the record either of the following statements
//depending on what your host provides
$file404 = $_SERVER['REDIRECT_ERROR_NOTES']."\n";
//or
$file404 = $_SERVER['REDIRECT_URL'] . "\n";
$filename = '/var/users/mysite/htdocs/history404.txt';

if (
file_exists($filename))
{
   
$filecontents = file($filename);
   
$add = (!in_array($file404, $filecontents));
}
else
$add = true;

if (
$add)
{
   
$fp = fopen($filename, 'a');
   
$write = fputs($fp, $file404);
   
fclose($fp);
   
$body = '';
   
$reportvars = array('REDIRECT_ERROR_NOTES','HTTP_HOST', 'HTTP_REFERER', 'HTTP_USER_AGENT', 'HTTP_FROM',
       
'REDIRECT_URL', 'REDIRECT_REQUEST_METHOD', 'REDIRECT_STATUS','REQUEST_URI','QUERY_STRING',
       
'REMOTE_ADDR', 'REMOTE_PORT');
    foreach(
$reportvars as $key) $body .= "$key: {$_SERVER[$key]}\n";
   
mail('webmaster@mysite.com','404:' . $_SERVER['REDIRECT_URL'],$body);
}

$revbits = explode('.',strrev($_SERVER['REDIRECT_URL']));

$pages['html'] = array('html','htm','php','txt');
$pages['special'] = array('css', 'js');

$ext = strrev($revbits[0]);
if (
in_array($ext, $pages['html']))
{
   
// or show a make a comment form
   
header('Location: /index.php');
}
elseif (
in_array($ext, $pages['special']))
{
   
//output nothing
   
echo '';
}
else
{
   
//throw a large image onto the screen so it's easy to hunt down.
    //change the width and height to a more discreet dimension when live
    //although if the img has height and width attributes that will overrule
    //the width and height here
   
$width = 50;
   
$height = 50;

   
$image  = ImageCreate($width,$height);
   
$blob = ImageColorAllocate($image,27,48,107);

    if (
function_exists("imagegif")) {
       
header ("Content-type: image/gif");
       
imagegif ($image);
    }
    elseif (
function_exists("imagejpeg")) {
       
header ("Content-type: image/jpeg");
       
imagejpeg ($image, "", 0.5);
    }
    elseif (
function_exists("imagepng")) {
       
header ("Content-type: image/png");
       
imagepng ($image);
    }
    elseif (
function_exists("imagewbmp")) {
       
header ("Content-type: image/vnd.wap.wbmp");
       
imagewbmp ($image);
    }
    else
       
// or whatever action should be taken if you don't have GD installed.
       
die("No image support in this PHP server");

   
ImageDestroy($image);
}
?>



DB Connection Function with error handling and email failure notices
Categories : PHP, MySQL, Errors and Logging, Databases, Errors and Logging
A simple function to prevent undefined $_POST/$_GET/$_SESSION variable errors
Categories : PHP, Variables, Errors and Logging
Visits-tracking
Categories : PHP, Databases, MySQL, Errors and Logging, Functions
Logging 404 errors in your custom statistics using Apache and a PHP script.
Categories : Apache, Web Servers, PHP, Errors and Logging
Ping a Server and run a command to fix it if it is down
Categories : PHP, Errors and Logging, Regexps
Error mailing logging facility
Categories : PHP, Errors and Logging, Email
Building a basic error handler with custom error types
Categories : PHP, PHP Classes, Errors and Logging
A Custom Error Handling And Debugging Class
Categories : PHP, PHP Classes, Debugging, Errors and Logging
Example of function to send out email if error occurs
Categories : PHP, Email, Debugging, Errors and Logging
Query2Report : Generating Html, Pdf and Csv Reports from SQL Query
Categories : PHP, PHP, HTML, PDF, Excel
error_log -- send an error message somewhere
Categories : PHP, PHP Functions, Errors and Logging
[PHP5] PHP Debugger and Helper
Categories : PHP, PHP Classes, Errors and Logging, Debugging, XML
Retrieve text from table and email to your e- address in pipe delimited format.
Categories : PHP, MySQL
Accepts a database & hostname from a user and then HTTP username and password. Uses this to connect to a MySQL database. Produces a form based on the tables it finds there to allow the user to do SELECTs, INSERTs, and DELETEs.
Categories : Databases, PHP, MySQL, Complete Programs
Check if a file exists on a remote FTP server with PHP
Categories : PHP, FTP, Regexps