29 March, 2012

Website Logger

These files can be included in your website code and can record who visits your website with a useragent, date, time, ip address, hostname, their current page and their referring page.

Ok First off Create these files

1) log.php // can change the names of the files
2) logfile.txt
3) viewlog.php


I will go through each one step by step so you know what each piece of code means.

First off log.php

PHP Code:
<?php
$ipaddress = $_SERVER['REMOTE_ADDR']; // fetch the users's ip address
$page = "http://{$_SERVER['HTTP_HOST']}{$_SERVER['PHP_SELF']}"; //the current page the user was on
$referrer = $_SERVER['HTTP_REFERER']; // the last page the user was on
$datetime = mktime(); // the DATE of the request
$useragent = $_SERVER['HTTP_USER_AGENT']; // Pretty obvious
$remotehost = @getHostByAddr($ipaddress); // This returns the host name of the Internet host specified by the variable $ipaddress
date_default_timezone_set("Europe/London"); // Sets the Default Timezone, for a list of timezones go to http://php.net/manual/en/timezones.php
$time = date("h:i:s A");
// Create log line
$logline = $ipaddress . '|' . $referrer . '|' . $datetime . '|'. $useragent . '|' . $remotehost . '|' . $page . '|' . $time . '| "';

// Write to log file:
$logfile = 'logfile.txt'; // You can change this to any name you want

// Open the log file in "Append" mode
if (!$handle = fopen($logfile, 'a+')) {
die("Failed to open log file");
}

// Write $logline to our logfile.
if (fwrite($handle, $logline) === FALSE) {
die("Failed to write to log file");
}

fclose($handle);
?>

And thats the Log.php file, Pretty neat, and tidy i think.

Ok if your server doesn't support creating files using scripts, create one manually and set the name as you would in log.php

Now onto viewlog.php
This is where you view your logs, i recommend password protecting this with either .htaccess or additional protection.

PHP Code:
<?php

// Define your username and password
$username = "USERNAME";  // Change Me
$password = "PASSWORD"; // Change Me

if ($_POST['txtUsername'] != $username || $_POST['txtPassword'] != $password) {

?>
<html>
<head>
</head>
<body>
<center>
<h2>Super Secret Control Panel</h2>
<h1>Login</h1>

<form name="form" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
    <p><label for="txtUsername">Username:</label>
    <br /><input type="text" title="Enter your Username" name="txtUsername" /></p>

    <p><label for="txtpassword">Password:</label>
    <br /><input type="password" title="Enter your password" name="txtPassword" /></p>

    <p><input type="submit" name="Submit" value="Login" /></p>

</form>
</center>
</body>
</html>
<?php

}
else {

?>
<?php
$logfile = "logfile.txt";

if (file_exists($logfile)) {

$handle = fopen($logfile, "r");
$log = fread($handle, filesize($logfile));
fclose($handle);
} else {
die ("The log file doesn't exist!");
}


// Seperate each logline
$log = explode("
", trim($log));

// Seperate each part in each logline
for ($i = 0; $i < count($log); $i++) {
$log[$i] = trim($log[$i]);
$log[$i] = explode('|', $log[$i]);
}
// Show a table of the logfile
?>
<html>
<head>
</head>
<body>
<div id="container">
<div id="content">
<table cellspacing="1" cellpadding="1" size="1800px">
<th>IP Address</th>
<th>Referrer</th>
<th>Current Page</th>
<th>Date</th>
<th>Time - GMT</th>
<th>Useragent</th>
<th>Remote Host</th>

<?
foreach ($log as $logline) {
$text = urldecode($logline['1']);
$newtext = wordwrap($text,40,"\n", true);

echo '<tr>';

echo '<td>'. $logline['0'] .'</td>';
echo '<td>'.$newtext.'</td>';
echo '<td>' . $logline['5'] . '</td>';
echo '<td>' . date('d/m/Y', $logline['2']) . '</td>';
echo '<td>' .  $logline['6'] . '</td>';
echo '<td>' . $logline['3'] . '</td>';
echo '<td>' . $logline['4'] . '</td>';


echo '</tr>';

}
?>
</table>
</div>
</div>
</body>
</html>

<?php

}

?>

And that is it.

Upload it to your webserver and make sure log.php is in the root directory of your html/www folder and use the following code to include it in ALL of the web documents you want to record.
PHP Code:
<?
include ('log.php');
?>

And that's it :)

I don't want Reps or whatnot, I just want to help people that have had spammers etc etc onto their site.

Enjoy and reply if you find this helpful :)

Labels:

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home