Page 1 of 2
A textfile based counter script's efficiency???
Posted: Thu Sep 05, 2002 9:31 am
by lc
Lo peoples.
I was thinking... I do some sites for a few people on which I put free counters. And I already did some pageview counter scripts and such.
But I suddenly realised how easy it would be to create a textfile based visitor counter that stores/checks every IP and adds 1 to a counter file in case it's new.
But here's my question.... say you have a site that has (not too many) but still like 25.000 visitors a year. I mean... 25.000 lines in a textfile to check every time a page loads seems like a lot to me.
What do you think? Would it be completely inefficient, bog down your site, and plain not a good idea. Or since it's rather nice to have it in your own controll, you'd go for it.
The big question is.... How much work is it for php to check that many lines from a flatfile. I've written big scripts before, but that's like 2000 lines of script working on rather small textfiles, this script would be tiny but have a lot of work to do.
Posted: Thu Sep 05, 2002 10:16 am
by Takuma
Don't use files cos if 2 users access the page at the same time... It's not gonna work... Well you can use flock though if you want to use it.
Posted: Thu Sep 05, 2002 10:20 am
by lc
of course I'd flock it... goes without saying. and a site that gets maybe 200 visitors a day max shouldn't have a lot of problems with that.
I think I may try it out on some not so often visited sites on which it should be fine... but what about the larger ones???
Posted: Thu Sep 05, 2002 10:34 am
by gite_ashish
hi,
To avoid the problems related with file locking (if u have tooo many visitors), reading big text file etc... go for database (mysql) !!
And anyway... i think the mysql solutions will always work efficiently.
This is the standard flat file v/s database stuff... in which for big records, like in our case, the db is bound to be more best sutiable solution.
you can have simple db structure something like:
domain_ip varchar(100) // value can be domain/ip... whatever u want
count unsigned int
on visit of any page, the php counter will first query the domain/ip in the db:
If it exists: increment the count
If it does not exists: insert the new record with 1 as the count
Posted: Thu Sep 05, 2002 10:36 am
by Takuma
Would ODBC be better than MySQL?
Posted: Thu Sep 05, 2002 10:53 am
by mikeq
Takuma wrote:Would ODBC be better than MySQL?
ODBC isn't a database, it is a set of drivers for connecting to a database, you would still need a database regardless of the drivers you use.
Posted: Thu Sep 05, 2002 11:17 am
by Takuma
Sorry I meant MS Access etc. All that stuff.
P.S. How do you set up ODBC?
Posted: Thu Sep 05, 2002 1:45 pm
by lc
gite_ashish wrote:(if u have tooo many visitors)
Right well I think we all know that. But I'm saying... I have no mysql available, so if I'm going to DIY this then it'll be flatfile.
When is it too many??? guestimate me something. How big a textfile starts to really slow down the server??
I'm really slightly ticked at dodgy tracking services, I just had 3 sites loose all data the other day.
Posted: Thu Sep 05, 2002 1:52 pm
by hob_goblin
try this:
have two files, one to store a date, and one to store the ip's...
then do a script like this: open the date file, check if it is x days away from the current date, and if it isn't, add the IP to the other file on a new line. if it is, open the other file in "w" mode and overwrite it, putting the IP on the first line also writing a new date to the date file. then do this to echo the hits:
Code: Select all
$count = file("logofips.txt");
echo count($count);
make sure to check if the IP is already in the log of ip's before adding the ip to the log if it isn't x days after the date in the date file
Posted: Thu Sep 05, 2002 1:54 pm
by m3mn0n
there pretty simple:
counter.php
Code: Select all
<?php
//this is our text file if you create a different
//named file change the script below to reflect this
$counter_file = ("counter.txt");
//now we open the file
$visits = file($counter_file);
//this increments the counter value by 1
$visitsї0]++;
//now we will open the counter file for
//writing "w"
$fp = fopen($counter_file , "w");
//put the new count value into the counter
//file
fputs($fp , "$visitsї0]");
//close the file
fclose($fp);
//display the count
echo ($visitsї0] );
?> Hits
then on the page you want it to display on:
Code: Select all
<?
include ("counter.php");
?>
Posted: Thu Sep 05, 2002 3:44 pm
by lc
Argh being misunderstood
Oromain.. thx but that is a pageview script much like a couple I already have and yes it works fine but it adds 1 to the count every time the script is run... not +1 every time someone who hasn't visited yet is runs it.
I'm not asking for script suggestions, but a simple guestimate of how much visitors is too much for flatfile tracking.
I can write the script myself no problem. Just first do a check of a stored IP and if it does not exist add it in and indeed run a script like oromain's.
Posted: Thu Sep 05, 2002 4:27 pm
by m3mn0n
lol~ sorry i did'nt read it all
I would say that depends on how you are loggin visitors and how much space you an afford to give to the counter. Sorry i got no estimations.

Posted: Thu Sep 05, 2002 5:28 pm
by hob_goblin
if the file is 50k, and 25 people access it at a time - thats 1.25 megs of RAM used up by just a counter
it will be pretty damn slow... i'd reccommend finding a different approach
Posted: Thu Sep 05, 2002 6:30 pm
by anilva
Hi,
I'm not an expert programmer.
But if you worry about the performance while the text file contains the domain/ip gets huge, Isn't it appropriate to split the file? Something like this -
domain names starts with a to j - first.txt, k to t - second.txt, u to z - third.txt.
So depends on the domain name of the visitor, you can choose which file to write/append and this will keep the file size smaller.
Just a suggestion-
Posted: Thu Sep 05, 2002 6:47 pm
by hob_goblin
domain names? it would be pointless to do that, you'd still have to count each line in each file