PHP custom error page + report


SUBMITTED BY: Guest

DATE: Oct. 23, 2013, 6:35 a.m.

FORMAT: PHP

SIZE: 2.7 kB

HITS: 1075

  1. <?php
  2. /* create a .htaccess file in your root and enter these most common error code definitions
  3. ErrorDocument 400 /error.php?err=400
  4. ErrorDocument 401 /error.php?err=401
  5. ErrorDocument 403 /error.php?err=403
  6. ErrorDocument 404 /error.php?err=404
  7. ErrorDocument 500 /error.php?err=500
  8. */
  9. // save this code below in file name "error.php"
  10. if (isset($_GET['err'])) {
  11. $errorNum = $_GET['err'];
  12. } else {
  13. $errorNum = "undef. number";
  14. }
  15. $emailaddress = "your@mail.com";
  16. /*
  17. you have to create this file and enter there all URL's with bad links and
  18. all IP addresses which are hot linking not existing files, example:
  19. 146.34.34.110
  20. http://www.somedomain.com/filename.htm
  21. */
  22. $filename = "http://".$_SERVER['HTTP_HOST']."/blocked_ref.txt";
  23. // if the http referer is not empty check the file for exsiting URL's
  24. if (!empty($_SERVER['HTTP_REFERER'])) {
  25. $bad_ref = file ($filename);
  26. $bad_counter = 0;
  27. foreach ($bad_ref as $val) {
  28. if (substr($_SERVER['HTTP_REFERER'], 0, 20) == substr($val, 0, 20)) {
  29. $bad_counter++;
  30. }
  31. }
  32. if ($bad_counter > 0) {
  33. header("Location: http://".$_SERVER['HTTP_HOST']);
  34. die();
  35. } else {
  36. $errortime = (date("d M Y h:m:s"));
  37. $message = $errorNum." Error Report\r\n\r\nA ".$errorNum." error was encountered by ".$_SERVER['REMOTE_ADDR'];
  38. $message .= " on $errortime.\r\n\r\n";
  39. $message .= "The URI which generated the error is: \nhttp://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']."\r\n\r\n";
  40. $message .= "The referring page was:\n".$_SERVER['HTTP_REFERER']."\r\n\r\n";
  41. $message .= "The used client was:\n".$_SERVER['HTTP_USER_AGENT']."\r\n\r\n";
  42. $headers = "From: ".$emailaddress."\nDate: ".$errortime." +0100\n";
  43. $subject = "Error: ".$errorNum." from ".$_SERVER['HTTP_REFERER'];
  44. mail($emailaddress, $subject, $message, $headers);
  45. }
  46. } else { // at last check if there are already some bad IP Addresses
  47. $bad_ref = file ($filename);
  48. $very_bad_counter = 0;
  49. foreach ($bad_ref as $val) {
  50. if (substr($_SERVER['REMOTE_ADDR'], 0, 10) == substr($val, 0, 10)) {
  51. $very_bad_counter++;
  52. }
  53. }
  54. if ($very_bad_counter > 0) {
  55. header("Location: http://www.google.com"); // or some other nice URL
  56. die();
  57. }
  58. }
  59. // place here the html code you want to show the visitor, like
  60. echo "<p>Some error...</p>";
  61. ?>

comments powered by Disqus