Friday, October 5, 2012

Custom Error Page with .htaccess PHP

10/05/2012

Create your own custom error page

This custom error page shows some standard error information and reports the error to the webmaster by e-mail. The script works for dead links and will report expired hot links from other sites (the IP address). Just add bad URL's and IP addresses into a text file to get the errors only once.


Create a .htaccess file in your root and enter these most common error code definitions


ErrorDocument 400 /error.php?err=400
ErrorDocument 401 /error.php?err=401
ErrorDocument 403 /error.php?err=403
ErrorDocument 404 /error.php?err=404
ErrorDocument 500 /error.php?err=500




Save below code in file "error.php"

<?php
/* 
Online PHP Examples with Source Code
website: http://4evertutorials.blogspot.in/
*/


if (isset($_GET['err'])) {
$errorNum = $_GET['err'];
} else {
$errorNum = "undef. number";
}

$emailaddress = "your@mail.com";
/*
you have to create this file and enter there all URL's with bad links and
all IP addresses which are hot linking not existing files, example:
117.40.85.210
http://www.example.com/filename.htm
*/
$filename = "http://".$_SERVER['HTTP_HOST']."/blocked_referer.txt";

// if the http referer is not empty check the file for exsiting URL's
if (!empty($_SERVER['HTTP_REFERER'])) {
$bad_referer = file ($filename);
$bad_counter = 0;
foreach ($bad_referer as $val) {
if (substr($_SERVER['HTTP_REFERER'], 0, 20) == substr($val, 0, 20)) {
$bad_counter++;
}
}
if ($bad_counter > 0) {
header("Location: http://".$_SERVER['HTTP_HOST']);
die();
} else {
$errortime = (date("d M Y h:m:s"));
$message = $errorNum." Error Report\r\n\r\nA ".$errorNum." error was encountered by ".$_SERVER['REMOTE_ADDR'];
$message .= " on $errortime.\r\n\r\n";
$message .= "The URI which generated the error is: \nhttp://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']."\r\n\r\n";
$message .= "The referring page was:\n".$_SERVER['HTTP_REFERER']."\r\n\r\n";
$message .= "The used client was:\n".$_SERVER['HTTP_USER_AGENT']."\r\n\r\n";
$headers = "From: ".$emailaddress."\nDate: ".$errortime." +0100\n";
$subject = "Error: ".$errorNum." from ".$_SERVER['HTTP_REFERER'];
mail($emailaddress, $subject, $message, $headers);
}
} else { // at last check if there are already some bad IP Addresses
$bad_referer = file ($filename);
$very_bad_counter = 0;
foreach ($bad_referer as $val) {
if (substr($_SERVER['REMOTE_ADDR'], 0, 10) == substr($val, 0, 10)) {
$very_bad_counter++;
}
}
if ($very_bad_counter > 0) {
header("Location: http://4evertutorials.blogspot.in/"); // or some other nice URL
die();
}
}
// place here the html code you want to show the visitor, like
echo "you do not have sufficient permissions to access this page...

";



?>

helpful? Share this

The Editorial Team of 4everTutorials consists of a group of PHP Professionals.

0 comments:

 

© 2014 4everTutorials. All rights resevered.

Back To Top