A textfile based counter script's efficiency???
Moderator: General Moderators
A textfile based counter script's efficiency???
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.
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.
- gite_ashish
- Forum Contributor
- Posts: 118
- Joined: Sat Aug 31, 2002 11:38 am
- Location: India
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
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
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.gite_ashish wrote:(if u have tooo many visitors)
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.
- hob_goblin
- Forum Regular
- Posts: 978
- Joined: Sun Apr 28, 2002 9:53 pm
- Contact:
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:
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
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);
Last edited by hob_goblin on Thu Sep 05, 2002 1:55 pm, edited 1 time in total.
there pretty simple:
counter.php
then on the page you want it to display on:
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] );
?> HitsCode: Select all
<?
include ("counter.php");
?>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.
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.
- hob_goblin
- Forum Regular
- Posts: 978
- Joined: Sun Apr 28, 2002 9:53 pm
- Contact:
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-
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-
- hob_goblin
- Forum Regular
- Posts: 978
- Joined: Sun Apr 28, 2002 9:53 pm
- Contact: