PHP Counter

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

rafi
Forum Newbie
Posts: 19
Joined: Wed Nov 24, 2004 9:36 am

PHP Counter

Post by rafi »

Hi .
I've been looking everywhere for a good php counter..
I'm looking for a counter..that users can view the counter from a diffrent page..
anyone knows how to make a counter like this ?
index.php?action=downloadfile&filename=
I mean like , include.
please.
thanks..
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

What are you counting? Downloads? Page views?
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
rafi
Forum Newbie
Posts: 19
Joined: Wed Nov 24, 2004 9:36 am

Post by rafi »

Yeah mostly downloads..
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

To do something like that, you'll most likely need to route all downloads through one file (like download.php). If you pass that file a filename like you did in your example, download.php can then log the fact that it was downloaded. If you store that in a database, it'll be easy to retrieve the download counts for each file.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
rafi
Forum Newbie
Posts: 19
Joined: Wed Nov 24, 2004 9:36 am

Post by rafi »

Yeah , I see.
problem is I'm new to PHP.
and i've got no idea how to do that.
rehfeld
Forum Regular
Posts: 741
Joined: Mon Oct 18, 2004 8:14 pm

Post by rehfeld »

hotscripts.com
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post by josh »

rehfeld wrote:hotscripts.com
Or you could guide him in the right direction to code it himself....


-------------------

Play around in mysql a bit and get the feel for databases, then go to http://php.net and search for "mysql".... look at some of the functions you find.

So you could do something like this:


download.php

Code: Select all

<?php
$file=$_GET['file'];
$file=addslashes($file);

/* Do a mysql query here to Insert / update something to show that some one has downloaded $file */

header("location:http://yoursite.com/downloads/$file");

?>
So when some one goes to download.php?file=program.exe it first changes the value in the database then it fowards them to the actual file...

or instead of fowarding to the file:

Code: Select all

<?php
header('Content-type: application/x-msdownload');
output("downloads/" . $file . ".exe");
?>
rafi
Forum Newbie
Posts: 19
Joined: Wed Nov 24, 2004 9:36 am

Post by rafi »

hey thanks! , i'll try.
User avatar
asi0917
Forum Commoner
Posts: 41
Joined: Thu Nov 25, 2004 10:37 am
Location: Shoreline, Washington
Contact:

Post by asi0917 »

rafi
Forum Newbie
Posts: 19
Joined: Wed Nov 24, 2004 9:36 am

Post by rafi »

Ok i've got it working.
http://www.oktober.co.il/rafi/files/ind ... e=test.ace
and the counter works good too..(counter.dat)
i want to to count more files though..
like ?name=bleh.zip..i want the counter to be like counter2.txt..
is it possible to have seprate download counters?
thanks alot..
here's the code for index.php..

<?php
// download.php?name=filename
header("Content-type: application/force-download");
header("Content-Disposition: attachment; filename=".$_GET["name"]);
@readfile('./'.$_GET['name']);

if(file_exists("counter.dat"))
{
$exist_file = fopen("counter.dat", "r");
$new_count = fgets($exist_file, 255);
$new_count++;
fclose($exist_file);
print("$new_count people have visited this page");
$exist_count = fopen("counter.dat", "w");
fputs($exist_count, $new_count);
fclose($exist_count);
}
else
{
$new_file = fopen("counter.dat", "w");
fputs($new_file, "1");
print("1 person have visited this page");
fclose($new_file);
}
?>
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post by josh »

good job but use [ p h p ] tags around your code so we can read it
rafi
Forum Newbie
Posts: 19
Joined: Wed Nov 24, 2004 9:36 am

Post by rafi »

Oh , you can't read it now ?
Deemo
Forum Contributor
Posts: 418
Joined: Sun Jan 18, 2004 11:48 am
Location: Washington DC

Post by Deemo »

no we can read it, just the

Code: Select all

tags makes it easier to read
User avatar
dull1554
Forum Regular
Posts: 680
Joined: Sat Nov 22, 2003 11:26 am
Location: 42:21:35.359N, 76:02:20.688W

Post by dull1554 »

Code: Select all

<?php
// download.php?name=filename
header("Content-type: application/force-download");
header("Content-Disposition: attachment; filename=".$_GET["name"]);
@readfile('./'.$_GET['name']);

if(file_exists("counter.dat"))
{
$exist_file = fopen("counter.dat", "r");
$new_count = fgets($exist_file, 255);
$new_count++;
fclose($exist_file);
print("$new_count people have visited this page");
$exist_count = fopen("counter.dat", "w");
fputs($exist_count, $new_count);
fclose($exist_count);
}
else
{
$new_file = fopen("counter.dat", "w");
fputs($new_file, "1");
print("1 person have visited this page");
fclose($new_file);
}
?>
rafi
Forum Newbie
Posts: 19
Joined: Wed Nov 24, 2004 9:36 am

Post by rafi »

uhm..weird..the code you posted doesn't do anything ...
you mean to put that instead of the existing download.php..
hmm..it didnt do anything..
not even count..
are you sure about that code?
thanks!...
Post Reply