Help - Search - Members - Calendar
Full Version: Hit Counter
Zymic Webmaster Forums > Web Design & Development > Server Side Scripting > PHP
Colin
Hey...I have been searching internet for like 30 min. and there is not one hit counter that I need lol here is what I want:
Total Hits
unique Hits
Users Online
Those are the must-haves lol ...what would be nice is a hits Today but its not that important lol...you all know any?
MrTouz
Ok this is the one i have wich gives me : All time page hits / All time unique hits / todays hits / todays unique hits

Step 1 :

In your MAIN path of your website (so the stats follow the WHOLE site) create a folder and name it what ver you want, i named it sitestats CHMODE it to 755.

Step 2 :

Copy past this code inside a .php document

CODE
<body>

<!-- You can change the color of the font by altering the below code -->
<font face="Verdana" size="1" color="#D4D3AE""><body bgcolor="#2F2D21">

<?php
// The log file;
$counter = "./sitestats/stats.txt";

// Date logging;
$today = getdate();
$month = $today[month];
$mday = $today[mday];
$year = $today[year];
$current_date = $mday . $month . $year;


// Log visit;
$fp = fopen($counter, "a");
$line = $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 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));


// 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));


?>

<?
// The echo display in which people will see;
echo "
Page hits :<b> " . $total_hits . "</b><br><br>
Unique hits : <b> " . $total_hosts_size . "</b><br><br>
Todays Page hits : <b> " . $daily_hits_size . "</b><br><br>
Todays unique hits : <b>" . $daily_hosts_size; "</b>"
?>
     </font>
    
</body>


You can edit many things here which is obvious as you can read ! like the orders of ECHO or even on the top the color of tables or text....

MUST EDIT : at least if you changed the folder name and didnt put sitestats like me.. than edit :

CODE
// The log file;
$counter = "./sitestats/stats.txt";


this is going to create a (i think what you guys cal....) a flat file wich is going to store the IP's per day. (if it doesnt create it (i just cant remember) than just create it your self. THAN CHMODE AT 755

Once you edited the .php file just save as what ever you want and put this php FILE next to the folder you created (for me its sitestats) DO NOT put it inside of it :/

than you can view this in your browser and it should count everything.

TIP : At first use to have all .html pages so i couldnt put this code inside my files so i had to IFRAME the .php page with the php code in it smile.gif so you can iframe this page so it can go to your .html files aswell smile.gif

Good Luck, sorry if i cant be understand, im not a reall tutorial writter ...

To be honest.. this is the only one that worked for me ! i was liek you 6 month ago.. never found anything that worked without databases or much editing...

If you need any more help, do NOT hesitate smile.gif
Alex
It's worth noting that if you have even a mildly popular website this is going to grind to a halt fairly quickly, even with logfile rotation in my experience. So it would be better to migrate to a DBMS with all of the optimisations and efficiency those imply.

My advice, ignore the code above (it uses short-tags anyway, which immediately makes it dangerously non-portable. There are also some fairly stupid mistakes which will cause parse errors), and write one which uses MySQL or similar. Record IP addresses to identify uniques, and timestamps to determine how many are online at once (you could take every unique IP address on in the last ten minutes or something).
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2012 Invision Power Services, Inc.