Request: Hit counter
by Dan the 10. August, 2008This was a request from Peter and it's a small hit counter script that i used a little while ago. It used to be on the forum, but after the merge with pwnpwnpwn.com it's not there anymore.
But if there is a tutorial that you saw on the forum you want back please post a comment.
1.
Open a new document called stats.php and paste this code in the document:
<?php
// The file that it logs the Ip-adress;
$counter = "stats/stats.txt";
// Logs the date;
$today = getdate();
$month = $today[month];
$mday = $today[mday];
$year = $today[year];
$current_date = $mday . $month . $year;
// Log visit;
$fp = fopen($counter, "a");
$line = $_SERVER['REMOTE_ADDR'] . "|" . $mday . $month . $year . "\n";
$size = strlen($line);
fputs($fp, $line, $size);
fclose($fp);
// Read log file into array;
$contents = file($counter);
// Total hits;
$total_hits = sizeof($contents);
// Total different ip-adresses, total hosts;
$total_hosts = array();
for ($i=0;$i<sizeof($contents);$i++) {
$entry = explode("|", $contents[$i]);
array_push($total_hosts, $entry[0]);
}
$total_hosts_size = sizeof(array_unique($total_hosts));
// Dailyl different ip-adresses, daily hits;
$daily_hits = array();
for ($i=0;$i<sizeof($contents);$i++) {
$entry = explode("|", $contents[$i]);
if ($current_date == chop($entry[1])) {
array_push($daily_hits, $entry[0]);
}
}
$daily_hits_size = sizeof($daily_hits);
// Daily hosts;
$daily_hosts = array();
for ($i=0;$i<sizeof($contents);$i++) {
$entry = explode("|", $contents[$i]);
if ($current_date == chop($entry[1])) {
array_push($daily_hosts, $entry[0]);
}
}
$daily_hosts_size = sizeof(array_unique($daily_hosts));
?>
<?php echo "
<strong>Total Unique hits:</strong> " . $total_hosts_size . "
<strong>Unique hits today:</strong> " . $daily_hosts_size . "
<strong>Total hits:</strong> " . $total_hits . "
<strong>Daily hits:</strong> " . $daily_hits_size . " ";
?>
// The file that it logs the Ip-adress;
$counter = "stats/stats.txt";
// Logs the date;
$today = getdate();
$month = $today[month];
$mday = $today[mday];
$year = $today[year];
$current_date = $mday . $month . $year;
// Log visit;
$fp = fopen($counter, "a");
$line = $_SERVER['REMOTE_ADDR'] . "|" . $mday . $month . $year . "\n";
$size = strlen($line);
fputs($fp, $line, $size);
fclose($fp);
// Read log file into array;
$contents = file($counter);
// Total hits;
$total_hits = sizeof($contents);
// Total different ip-adresses, total hosts;
$total_hosts = array();
for ($i=0;$i<sizeof($contents);$i++) {
$entry = explode("|", $contents[$i]);
array_push($total_hosts, $entry[0]);
}
$total_hosts_size = sizeof(array_unique($total_hosts));
// Dailyl different ip-adresses, daily hits;
$daily_hits = array();
for ($i=0;$i<sizeof($contents);$i++) {
$entry = explode("|", $contents[$i]);
if ($current_date == chop($entry[1])) {
array_push($daily_hits, $entry[0]);
}
}
$daily_hits_size = sizeof($daily_hits);
// Daily hosts;
$daily_hosts = array();
for ($i=0;$i<sizeof($contents);$i++) {
$entry = explode("|", $contents[$i]);
if ($current_date == chop($entry[1])) {
array_push($daily_hosts, $entry[0]);
}
}
$daily_hosts_size = sizeof(array_unique($daily_hosts));
?>
<?php echo "
<strong>Total Unique hits:</strong> " . $total_hosts_size . "
<strong>Unique hits today:</strong> " . $daily_hosts_size . "
<strong>Total hits:</strong> " . $total_hits . "
<strong>Daily hits:</strong> " . $daily_hits_size . " ";
?>
Now save the file in a folder http://yoursite.com/stats/
2.
Then paste the this code
<?php include "stats/stats.php"; ?>
into the page where you want the hit counter to apear.
3.
The code will apear like this:
Total Unique hits: 1354
Unique hits today: 50
Total hits:1651
Daily hits: 114

