PDA

View Full Version : MySQL counter


Dalziel
07-08-2003, 04:17 PM
I've currently got a counter script on my site that is rather big written in PHP.

I have designed the site so that it can be updated by someone else who knows only a bit of basic HTML so the counter is hidden in a footer file, so the code has to be the same on every page and the script makes the files rather than the person making the page creating them.

It does this on every page

$counter_date = "03/05/2002";
This is updated whenever the counter is reset by deleting the counters directory.

if (!is_dir("counters")
Create directory counters

if (!file_exists("counters/all.txt"))
Create file called all.txt with just a 0 in it.

if (!file_exists("counters/$filename.txt"))
As with all.txt but $filename is the name of the document so in index.php $filename would be index.txt

fread on all.txt and $filename.txt reads the number then adds one to it and writes it back to the file.

Use javascript to display in the status bar:

"Since $counter_date this page has been viewed $filename.txt times, the site has received all.txt page views";

This works but it slows the pages down considerably. Someone told me using MySQL it would be faster and easier, I haven't got the first idea where to start or how to do it and I don't know the advantages, can anyone help?

raf
07-08-2003, 06:04 PM
Do you wan't to code it yourself or use a premade PHP classe ?

You'll find about 30 premade classes here
http://www.phpclasses.org/search.html?words=counter&go_search=1

For textfiles or db.
Frankly, i don't know if a db will be that faster then a txt file. But if you use a db for other functionalitys of the site, then you might as well use it for the counter. But i think that it probably something in your code that is creating the delay (filelocking or so).

My opinion is that if it's slowing down your site, then dump it. Counters are only usefull for the webmaster, to see which sections of the site are visited frequently and which aren't. Or to see i promotions etc reach an audience. But to a user? I'm not interested in how many visitors viewed the page before me. But that could be just me ...

Dalziel
07-08-2003, 07:35 PM
I havent used DB for anything else and I don't know where to start but since I know how many hits I'm getting without using that I will probably bin it.