#1 2006-11-09 08:21:22Error 404 logging - another realtime monitor
mobtex
Member

From: Loughborough, United Kingdom
Registered: 2006-04-14
Posts: 805
I've been thanked 13 times.
Online

Member
From: Loughborough, United Kingdom
Registered: 2006-04-14
Posts: 805
I've been thanked 13 times.
Online
I was bored with the standard cpanel error log so made this:
It's good to share. Enjoy! Hope it comes in handy to someone.
Variations on a theme of RTOC.....
Code: php
<?php
/* simple monitor for watching which urls are causing file not found errors for your users
step 1 - create table
CREATE TABLE `ERRORLOG` (
`id` smallint(6) NOT NULL auto_increment,
`url` text NOT NULL,
`date` timestamp(14) NOT NULL,
PRIMARY KEY (`id`),
KEY `date` (`date`),
FULLTEXT KEY `url` (`url`)
) TYPE=MyISAM COMMENT='404 logging ' AUTO_INCREMENT=1 ;
step 2 - fill in the configuration variables below
step 3 - upload the errorlog directory to your site's public html / htdocs folder
step 4 - configure .htaccess to handle error 404s
ErrorDocument 404 /errorlog/index.php?log
step 5 - goto www.yoursite.com/thisfilecantbefound/ to test
step 6 - goto www.yoursite.com/errorlog/?report to view the results */
// configuration variables
$host = "localhost"; // your database host - usually localhost
$uname = ""; // your username
$pword = ""; // your password
$database = ""; // your database
$table = "ERRORLOG"; // no need to change this unless you changed the table name
$results = "30"; // how many results to show when viewing the report
$homepage = "http://www.web2txt.co.uk/"; // your main url - include the http://
$seconds = "3"; // how many seconds to wait before redirecting a lost user
$daysdata = "30"; // how many days of data to store in the database
// there is no need to edit this script beyond this point unless you want to customise it to suit your own spec
// are we logging?
if(isset($_GET['log'])){
// setup the database
@mysql_connect("$host","$uname","$pword") or die('Failed to connect: '.mysql_error());
@mysql_select_db("$database") or die('Failed to select: '.mysql_error());
// which page was requested
$url = mysql_real_escape_string($_SERVER['REQUEST_URI']);
// log this to the database
$sql = "INSERT INTO `$table` (`id`,`url`,`date`) VALUES ('','$url',NOW())";
@mysql_query($sql) or die('Failed to insert record: '.mysql_error());
// delete any entries older than we wish to keep
$sql = "delete from $table where unix_timestamp() - unix_timestamp(date) > 60 * 60 * 24 * $daysdata";
@mysql_query($sql) or die('Failed to delete old record : '.mysql_error());
// show the user a simple page
echo "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">
<html>
<head>
<meta http-equiv=\"content-type\" content=\"text/html; charset=windows-1250\">
<meta http-equiv=\"refresh\" content=\"$seconds;url=$homepage\">
<title>We're sorry that file can't be found</title>
<style type=\"text/css\">
<!--
h1 { font-size: 12pt; font-family: Arial; font-weight: bold; color: #E72400 }
p { font-size: 10pt; font-family: Arial; color: #000000}
//-->
</style>
</head>
<body>
<h1>We're sorry that file can't be found</h1>
<p>
The file you have requested isn't there. Sorry this is our fault.
<br />
We have been notified of this error and apologise for the inconvience.
<br />
<br />
You will now be redirected to our <a href=\"$homepage\" title=\"Click to visit our main page\">main page</a>.
</p>
<!-- REQUEST_URI = $url -->
</body>
</html>";
$ok = "yes";
// close down the database
@mysql_close();
}
// are we showing the report
if(isset($_GET['report'])){
$feed = "http://".$_SERVER["HTTP_HOST"].$PHP_SELF."?feed";
$content = file_get_contents("$feed");
// show the page with the javascript callback
echo "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">
<html>
<head>
<title>Realtime Error Log Monitor</title>
<style type=\"text/css\">
<!--
h1 { font-size: 12pt; font-family: Arial; font-weight: bold; color: #E72400 }
p { font-size: 10pt; font-family: Arial; color: #000000}
//-->
</style>
</head>
<body>
<h1>Realtime Error Log Monitor</h1>
<p id=\"errorlog\">$content</p>
<script type=\"text/javascript\" language=\"javascript\">
if (window.XMLHttpRequest) {
xmlHttp = new XMLHttpRequest();
}else if(window.ActiveXObject){
xmlHttp = new ActiveXObject(\"Microsoft.XMLHTTP\");
}
function callServer(url) {
xmlHttp.open(\"GET\", url, true);
xmlHttp.onreadystatechange = updatePage;
xmlHttp.send(null);
}
function updatePage() {
if (xmlHttp.readyState == 4) {
var response = xmlHttp.responseText;
document.getElementById(\"errorlog\").innerHTML = response;
setTimeout(\"callServer('$feed')\",1000);
}
}
callServer(\"$feed\");
</script>
</body>
</html>";
$ok = "yes";
}
// are we just getting the data for the feed?
if(isset($_GET['feed'])){
// setup the database
@mysql_connect("$host","$uname","$pword") or die('Failed to connect: '.mysql_error());
@mysql_select_db("$database") or die('Failed to select: '.mysql_error());
// set headers in the past so this content is never cached
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
// get the data - there's only date and url so for once * is ok
$sql = "SELECT * FROM `$table` ORDER BY `date` DESC LIMIT $results";
$result = mysql_query($sql);
$num = mysql_numrows($result);
mysql_close();
while ($i < $num) {
$url = htmlentities(mysql_result($result,$i,"url"));
$date = htmlentities(mysql_result($result,$i,"date"));
echo "$url<br />\n";
++$i;
}
$ok = "yes";
// close down the database
@mysql_close();
}
if($ok!="yes"){
header("Location: $homepage");
exit();
}
?>
I know it's PHP but I thought it was good practice to monitor these things so bunged it here instead.
Andy Moore .mobi certified developer, web host and mobile media geek
NoSting Hosting - Brightside Hosting Ltd: Member of Nominet / eNom ETP
^^^ Now with over 400 free templates, ShoutCast, LAME + FFMPeg
Ringtones - Palace Marketing Ltd: D2C and B2B mobile content since 2002
NoSting Hosting - Brightside Hosting Ltd: Member of Nominet / eNom ETP
^^^ Now with over 400 free templates, ShoutCast, LAME + FFMPeg
Ringtones - Palace Marketing Ltd: D2C and B2B mobile content since 2002
| Never |
Active Discussions
Review Requests & Intros


