A textfile based counter script's efficiency???

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

lc
Forum Contributor
Posts: 188
Joined: Tue Apr 23, 2002 6:45 pm
Location: Netherlands

A textfile based counter script's efficiency???

Post 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.
User avatar
Takuma
Forum Regular
Posts: 931
Joined: Sun Aug 04, 2002 10:24 am
Location: UK
Contact:

Post 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.
lc
Forum Contributor
Posts: 188
Joined: Tue Apr 23, 2002 6:45 pm
Location: Netherlands

Post 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???
User avatar
gite_ashish
Forum Contributor
Posts: 118
Joined: Sat Aug 31, 2002 11:38 am
Location: India

Post 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
User avatar
Takuma
Forum Regular
Posts: 931
Joined: Sun Aug 04, 2002 10:24 am
Location: UK
Contact:

Post by Takuma »

Would ODBC be better than MySQL?
User avatar
mikeq
Forum Regular
Posts: 512
Joined: Fri May 03, 2002 3:33 am
Location: Edinburgh, Scotland

Post 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.
User avatar
Takuma
Forum Regular
Posts: 931
Joined: Sun Aug 04, 2002 10:24 am
Location: UK
Contact:

Post by Takuma »

Sorry I meant MS Access etc. All that stuff.

P.S. How do you set up ODBC?
lc
Forum Contributor
Posts: 188
Joined: Tue Apr 23, 2002 6:45 pm
Location: Netherlands

Post 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.
User avatar
hob_goblin
Forum Regular
Posts: 978
Joined: Sun Apr 28, 2002 9:53 pm
Contact:

Post 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
Last edited by hob_goblin on Thu Sep 05, 2002 1:55 pm, edited 1 time in total.
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post 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");
?>
lc
Forum Contributor
Posts: 188
Joined: Tue Apr 23, 2002 6:45 pm
Location: Netherlands

Post 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.
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post by m3mn0n »

lol~ sorry i did'nt read it all 8)

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. ;)
User avatar
hob_goblin
Forum Regular
Posts: 978
Joined: Sun Apr 28, 2002 9:53 pm
Contact:

Post 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
anilva
Forum Newbie
Posts: 3
Joined: Mon Aug 05, 2002 7:28 am
Location: Singapore

Post 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-
User avatar
hob_goblin
Forum Regular
Posts: 978
Joined: Sun Apr 28, 2002 9:53 pm
Contact:

Post by hob_goblin »

domain names? it would be pointless to do that, you'd still have to count each line in each file
Post Reply