PHP is open source scripting language. It\'s widely used to develop web applications.  Home Web Programming PHP A Nice Hit Counter

A Nice Hit Counter


A Nice Hit CounterI shows

Total Hits
Unique Hits
Todays Hits
Todays Unique Hits

1. Create a file called 'counter.php" and insert:

<?php
// todays stats;
$counter = "stats.txt";


$today = getdate();
$month = $today[month];
$mday = $today[mday];
$year = $today[year];
$current_date = $mday . $month . $year;
$fp = fopen($counter, "a");
$line = $REMOTE_ADDR . "|" . $mday . $month . $year . "n";
$size = strlen($line);
fputs($fp, $line, $size);
fclose($fp);
$contents = file($counter);
$total_hits = sizeof($contents);
$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));
$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 = 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));

?>
<? echo "
<b>Total Hits:</b> " . $total_hits . "<br>
<b>Unique Hits:</b>  " . $total_hosts_size . "<br>
<b>Todays Hits:</b>  " . $daily_hits_size . "<br>
<b>Todays Unique Hits:</b> " . $daily_hosts_size; 
?>

2. Now upload "counter.php" to your server and create a file called "stats.txt" and upload it to your server. CHMOD "stats.txt" to 777 and use this code to view the stats on your page

<?php include('counter.php'); ?>


Author's URL: Spacetech
PHP is open source scripting language. It\'s widely used to develop web applications. More PHP Tutorials: Featured Materials | Fresh Materials | More PHP Tutorials at LearnPHP.org

Reader's comments
comments Bridun September 03, 2011 says:
$line = $_SERVER["REMOTE_ADDR"]. "|" . $mday . $month . $year . $time . "rn";
Reply
comments ADY August 23, 2011 says:
Before $today = getdate(); add this line
$REMOTE_ADDR= getenv("REMOTE_ADDR");

and
replace $line = $REMOTE_ADDR . "|" . $mday . $month . $year . "n"; whit $line = $REMOTE_ADDR . "|" . $mday . $month . $year . "n";

Reply
comments Mohammed July 31, 2011 says:
$line = $REMOTE_ADDR . "|" . $mday . $month . $year . "n";

this works for me Big thanks for Creating a very Useful Codes

Reply
comments Vanja June 23, 2011 says:
..one backslash [] before the last n...
Reply
comments Vanja June 23, 2011 says:
But with a small correction [ just one before last n at the line below ]..and it works well..

$line = $REMOTE_ADDR . "|" . $mday . $month . $year . "n";

thank you, SpaceTech!

Reply
comments Vanja June 23, 2011 says:
Indeed..
Reply
comments Jas June 02, 2011 says:
Its not working :s thanx
Reply
Add comments to "A Nice Hit Counter"

Captcha