Page 1 of 2
PHP Counter
Posted: Wed Nov 24, 2004 9:41 am
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..
Posted: Wed Nov 24, 2004 9:43 am
by pickle
What are you counting? Downloads? Page views?
Posted: Wed Nov 24, 2004 9:56 am
by rafi
Yeah mostly downloads..
Posted: Wed Nov 24, 2004 10:14 am
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.
Posted: Wed Nov 24, 2004 10:24 am
by rafi
Yeah , I see.
problem is I'm new to PHP.
and i've got no idea how to do that.
Posted: Wed Nov 24, 2004 2:13 pm
by rehfeld
hotscripts.com
Posted: Wed Nov 24, 2004 10:51 pm
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");
?>
Posted: Thu Nov 25, 2004 10:56 am
by rafi
hey thanks! , i'll try.
Posted: Thu Nov 25, 2004 12:11 pm
by asi0917
Posted: Thu Nov 25, 2004 1:22 pm
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);
}
?>
Posted: Thu Nov 25, 2004 8:16 pm
by josh
good job but use [ p h p ] tags around your code so we can read it
Posted: Fri Nov 26, 2004 2:44 pm
by rafi
Oh , you can't read it now ?
Posted: Fri Nov 26, 2004 3:42 pm
by Deemo
no we can read it, just the
Posted: Fri Nov 26, 2004 5:05 pm
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);
}
?>
Posted: Fri Nov 26, 2004 5:23 pm
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!...