Jump to content

Hit counter help


avatar.alex

Recommended Posts

ok I don't want the page counter to recount the page each time me or anyone else roloads it or goes it it I just want one count per person could anyone help me out

 

heres the code thank you:

 

<?php
//-----------------------------------------------//
//---------------Simple Logger-------------------//
//-----------------------------------------------//
// This code is (c) to TheSavior (Eli White) as  //
// of 2005-2006. This code may NOT be reproduced //
// ,or                                           //
// distributed by any means, unless you have     //
// explicit written permissions by TheSavior     //
//-----------------------------------------------//
// You may edit this file as you wish, as long   //
// as it isn't redistributed after words.        //
// Free support is provided for all scripts on   //
// http://www.thesavior.co.nr                    //
//-----------------------------------------------//
// I hope you enjoy this script, now get to the  //
// fun part of looking at how it works! Its a    //
// joy. XD                                       //
//-----------------------------------------------//

//----------------------------------------------
// Log files with permissions to write
//----------------------------------------------
$log = "logger.txt";
$file = "users.txt";

//----------------------------------------------
// Get our variables for future use
//----------------------------------------------
$today = getdate();
$month = $today[month];
$mday = $today[mday];
$year = $today[year];
$date = $mday . $month . $year;
$remote = $_SERVER["REMOTE_ADDR"];
$timeoutsecs = 900;
$timestamp = time();
$timeout = ($timestamp-$timeoutsecs);
$online = fopen("$file", "a+");
$write = $remote."||".$timestamp."n";

//----------------------------------------------
// Write to our logfile
//----------------------------------------------
$fp = fopen($log, "a");
$line = "|" . $mday . $month . $year . "\n";
$size = strlen($line);
fputs($fp, $line, $size);
fclose($fp);

$contents = file($log);
$total_hits = count($contents);

//----------------------------------------------
// Formmating for daily_hits_size
//----------------------------------------------
$daily_hits = array();
for ($i=0;$i<count($contents);$i++) {
$entry = explode("|", $contents[$i]);
if ($date == rtrim($entry[1])) {
	array_push($daily_hits, $entry[0]);
}
}
$daily_hits_size = count($daily_hits);

//----------------------------------------------
// Users Online
//----------------------------------------------
fwrite($online, $write);
fclose($online);
$online_array = array();
$file_array = file($file);
foreach($file_array as $newdata)
{
list($ip, $time) = explode("||", $newdata);
if($time >= $timeout){
array_push($online_array, $ip);
}
}
$online_array = array_unique($online_array);
$online = 1+ count($online_array);

//----------------------------------------------
// Echo our stats to the page
//----------------------------------------------

echo 

"<span class=\"stats\">Page hits:<strong>" . $total_hits . "</strong><br />
Todays Page hits: <strong>" . $daily_hits_size . "</strong><br />
Users Online: <strong>" . $online . "</strong><br /></span>
"
?>

Link to comment
Share on other sites

Two ways:

 

1. you can count unique visits by IP addresses. If an IP address is already logged, don't log it.

2. use sessions. If a visit is being logged, set a session variable to 'logged' (or whatever you like). Don't log a visit when the session has that value.

Link to comment
Share on other sites

i just finished a project like that earlier today... this logs the IP's visiting your site

 

<?php
   function countIP()
   {
       $ip = $_SERVER['REMOTE_ADDR'];
       $chkIP = checkIP($ip);
       if (!$chkIP) {
           $write = $ip."\n";
           $fh = fopen('filename.txt', 'a+');
           fwrite($fh, $write);
           fclose($fh);
           return calcIP();
       } else {
           return calcIP();
       }
   }    

   function checkIP($ip)
   {
       $lines = file('filename.txt');
       foreach ($lines as $line) {
           if (trim($line) == $ip)
               return true;
       }
   }

   function calcIP()
   {
       $lines = file('filename.txt');
       $i = 1;
       foreach ($lines as $line) {
           $i++;
       }
       return $i-1;
   }
?>

 

edit it to your needs and wherever you want the counter just add this

<?php echo countIP(); ?>

 

EDIT: WTF happened to the code?!

Link to comment
Share on other sites

there's 2 ways to do that...

 

1. Make a new file for every day.

2. When today is over you clear the file and start over..

 

with option 1 you can keep track of visits and make visitor statistics

with option 2 you dont end up having hundreds of files

Link to comment
Share on other sites

thats cool well i think i got it but how would I add it so the it logs the ip address...

 

/
// Log files with permissions to write
$log = "logger.txt";
$file = "users.txt";


// Get our variables for future use
$today = getdate();
$month = $today[month];
$mday = $today[mday];
$year = $today[year];
$date = $mday . $month . $year;
$remote = $_SERVER["REMOTE_ADDR"];
$timeoutsecs = 900;
$timestamp = time();
$timeout = ($timestamp-$timeoutsecs);
$online = fopen("$file", "a+");
$write = $remote."||".$timestamp."n";


// Write to our logfile
$fp = fopen($log, "a");
$line = "|" . $mday . $month . $year . "\n";
$size = strlen($line);
fputs($fp, $line, $size);
fclose($fp);

$contents = file($log);
$total_hits = count($contents);


// Formmating for daily_hits_size
$daily_hits = array();
for ($i=0;$i<count($contents);$i++) {
$entry = explode("|", $contents[$i]);
if ($date == rtrim($entry[1])) {
	array_push($daily_hits, $entry[0]);
}
}
$daily_hits_size = count($daily_hits);

Link to comment
Share on other sites

Im looking through these two scripts and trying to put them together but im kinda stuck on where to put the parts....I think that I would take the

function checkIP($ip)

and change the $ip it $log but im not sure?

 

<?
function countIP()
    {
        $ip = $_SERVER['REMOTE_ADDR'];
        $chkIP = checkIP($ip);
        if (!$chkIP) {
            $write = $ip."\n";
            $fh = fopen('filename.txt', 'a+');
            fwrite($fh, $write);
            fclose($fh);
            return calcIP();
        } else {
            return calcIP();
        }
    }    
    
    function checkIP($ip)
    {


// Log files with permissions to write
$log = "logger.txt";
$file = "users.txt";


// Get our variables for future use
$today = getdate();
$month = $today[month];
$mday = $today[mday];
$year = $today[year];
$date = $mday . $month . $year;
$remote = $_SERVER["REMOTE_ADDR"];
$timeoutsecs = 900;
$timestamp = time();
$timeout = ($timestamp-$timeoutsecs);
$online = fopen("$file", "a+");
$write = $remote."||".$timestamp."n";


// Write to our logfile
$fp = fopen($log, "a");
$line = "|" . $mday . $month . $year . "\n";
$size = strlen($line);
fputs($fp, $line, $size);
fclose($fp);

$contents = file($log);
$total_hits = count($contents);


// Formmating for daily_hits_size
$daily_hits = array();
for ($i=0;$i<count($contents);$i++) {
$entry = explode("|", $contents[$i]);
if ($date == rtrim($entry[1])) {
	array_push($daily_hits, $entry[0]);

}
}
$daily_hits_size = count($daily_hits);

Link to comment
Share on other sites

well.. you have to add something like this

$chkIP = checkIP($remote);

 

before the section where the users IP address get's written to the file

 

then when the script is about to write the IP you run an IF statement to see if the IP was found or not.

if (!$chkIP) {
make the script add the IP
}

Link to comment
Share on other sites

ok I did it right I think but it says that there is a error on line 45 which is the line with the

 

 

$chkIP = checkIP($remote);

 

$log = "logger.txt";
$file = "users.txt";


// Get our variables for future use
$today = getdate();
$month = $today[month];
$mday = $today[mday];
$year = $today[year];
$date = $mday . $month . $year;
$remote = $_SERVER["REMOTE_ADDR"];
$timeoutsecs = 900;
$timestamp = time();
$timeout = ($timestamp-$timeoutsecs);
$online = fopen("$file", "a+");
$write = $remote."||".$timestamp."n";


// Write to our logfile

$chkIP = checkIP($remote);

if (!$chkIP) {


$fp = fopen($log, "a");
$line = "|" . $mday . $month . $year . "\n";
$size = strlen($line);
fputs($fp, $line, $size);
fclose($fp);

$contents = file($log);
$total_hits = count($contents);
}

// Formmating for daily_hits_size
$daily_hits = array();
for ($i=0;$i<count($contents);$i++) {
$entry = explode("|", $contents[$i]);
if ($date == rtrim($entry[1])) {
	array_push($daily_hits, $entry[0]);
}

Link to comment
Share on other sites

<?php
//-----------------------------------------------//
//---------------Simple Logger-------------------//
//-----------------------------------------------//
// This code is (c) to TheSavior (Eli White) as  //
// of 2005-2006. This code may NOT be reproduced //
// ,or                                           //
// distributed by any means, unless you have     //
// explicit written permissions by TheSavior     //
//-----------------------------------------------//
// You may edit this file as you wish, as long   //
// as it isn't redistributed after words.        //
// Free support is provided for all scripts on   //
// http://www.thesavior.co.nr                    //
//-----------------------------------------------//
// I hope you enjoy this script, now get to the  //
// fun part of looking at how it works! Its a    //
// joy. XD                                       //
//-----------------------------------------------//

//----------------------------------------------
// Log files with permissions to write
//----------------------------------------------
$log = "logger.txt";
$file = "users.txt";

//----------------------------------------------
// Get our variables for future use
//----------------------------------------------
$today = getdate();
$month = $today[month];
$mday = $today[mday];
$year = $today[year];
$date = $mday . $month . $year;
$remote = $_SERVER["REMOTE_ADDR"];
$timeoutsecs = 900;
$timestamp = time();
$timeout = ($timestamp-$timeoutsecs);
$online = fopen("$file", "a+");
$write = $remote."||".$timestamp."n";

//----------------------------------------------
// Write to our logfile
//----------------------------------------------
$chkIP = checkIP($remote);

if (!$chkIP) {

$fp = fopen($log, "a");
$line = "|" . $mday . $month . $year . "\n";
$size = strlen($line);
fputs($fp, $line, $size);
fclose($fp);

$contents = file($log);
$total_hits = count($contents);
}
//----------------------------------------------
// Formmating for daily_hits_size
//----------------------------------------------
$daily_hits = array();
for ($i=0;$i<count($contents);$i++) {
$entry = explode("|", $contents[$i]);
if ($date == rtrim($entry[1])) {
	array_push($daily_hits, $entry[0]);
}
}
$daily_hits_size = count($daily_hits);

//----------------------------------------------
// Users Online
//----------------------------------------------
fwrite($online, $write);
fclose($online);
$online_array = array();
$file_array = file($file);
foreach($file_array as $newdata)
{
list($ip, $time) = explode("||", $newdata);
if($time >= $timeout){
array_push($online_array, $ip);
}
}
$online_array = array_unique($online_array);
$online = 1+ count($online_array);

//----------------------------------------------
// Echo our stats to the page
//----------------------------------------------

echo 

"<span class=\"stats\">Page hits:<strong>" . $total_hits . "</strong><br />
Todays Page hits: <strong>" . $daily_hits_size . "</strong><br />
Users Online: <strong>" . $online . "</strong><br /></span>
"
?>

Link to comment
Share on other sites

it wants the function checkIP. you don't seem to have that function there, hence this is incomplete code (unless I'm blind. which I sometimes am).

 

 

can anyone comment, is storing hits in a textfile MUCH slower than an sql database? I would assume by alot!

 

also, keep in mind, people at offices/colleges many times use shared IP. therefore, if 30 people in an office look at your site and you filter by IP, you will get an very, very incorrect count. it's better to use sessions.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.