Forum Moderators: DixonJones

Message Too Old, No Replies

php counter needed

client wants me to track how many times file is downloaded

         

HelenDev

11:12 am on Mar 26, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Does anyone know how I can put a little counter on one page or to track one download link? I found a free script from Ask Webmaster which is as follows...

Create a blank text file (in notepad) and called it counter.txt and put " 0 " in it then chmod it to 777.

Put this code on your page:

php

$fp = fopen("counter.txt","r");

$count = fread ($fp, filesize ("counter.txt"));

fclose($fp);

$count++;

$fp = fopen("counter.txt","a+");

fputs($fp, $count);

fclose($fp);

echo "Hits: $count";

?>

... but I don't know how to "chmod to 777". I know this is some sort of Unix command but I am just FTPing my files up to a shared server. Can I still use this script, and how do I get my notepad file there?

Alternatively I know I could also just go through my log files to see how many times the file is requested, but is there a way do get php to do this for me?

Any help would be much appreciated. Boss looks in a bad mood today.

Helen.

HelenDev

12:16 pm on Mar 26, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I have now got the free script to work. All I had to do was drop the txt file in the same directory, duh!

"chmod to 777" - why can't they just say, put it in the same directory? Cuh.

Not sure about the feasability of getting a script to read my log file - it's huge! Worried I might break the internet ;)

RobBroekhuis

12:37 pm on Mar 26, 2004 (gmt 0)

10+ Year Member



I see no reason why you couldn't use a script to read the logfile, even if it's huge. Assuming you only need this information on a, say, once a day basis. Otherwise the running counter makes more sense.
Rob

HelenDev

1:36 pm on Mar 26, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Cheers Rob.

The counter is not running as well as I had hoped. It goes 1,2,13,1214,12131215 and I won't even tell you where it goes after that.

It's like one of those horrible which-number-is-next-in-the-sequence maths exam questions.

Also, the txt file seems to stay at 0 when I look at it.

I would like to get a script to read my logs, but I don't think I am putting the path right because it doesn't work. My logs are in a directory above the main public_html and I have put the path as

$fp = fopen("/home/mydomain/logs/access_log", "r");

but this doesn't seem to work.

Any thoughts?

RobBroekhuis

6:42 pm on Mar 26, 2004 (gmt 0)

10+ Year Member



So when you're accessing your account (Unix?) and you
type

cd /home/mydomain/logs

you get to the correct directory? I'd think that should work. In my case, the filename isn't just access_log, it has extensions to describe the date, e.g.

access.log.13.5

Are you sure yours is just "access_log"?

I read out my logs using php all the time (it's kind of addictive :-)
Rob

larryn

10:35 pm on Mar 26, 2004 (gmt 0)

10+ Year Member



When you open the counter file, you are setting it in the append mode (that the "a+" bit in the fopen), hence the '1', '12', '123', etc.

I think you want to overwrite the existing file, and that should give you only one number.

Larry