counter modification

Links for php scripts

Moderators: macek, egami, gesf

User avatar
TrickyRic
New php-forum User
New php-forum User
Posts: 50
Joined: Mon Mar 03, 2003 6:28 am
Location: Toronto, Canada
Contact:

counter modification

Postby TrickyRic » Sat Jul 26, 2003 5:04 am

hi me again, im still working on my freefornow site ( http://freefornow.dyndns.org/freefornow ) and have decided to add counters showing how many times each file gets downloaded, i found and modified some counter code which works and is included with this post, (also viewable here http://freefornow.dyndns.org/freefornow/test.php ) but i need someone with more experiance than me to try and modify it so it only incrememnts when the link to download a file is clicked. heres the code...

<HTML>
<HEAD>
<TITLE>Untitled</TITLE>
<META NAME="GENERATOR" CONTENT="HTMLIATE">
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
</HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000">
<?php
function Counter($counterfile='counter.dat') {
if (file_exists($counterfile))
{
$fp = fopen($counterfile, 'r');
$num = fread($fp, 100);
$cnum = ($num + 1);
fclose($fp);

$fa = fopen($counterfile, 'w');
fwrite( $fa, $cnum, 100);
fclose($fa);

$counter = $cnum;
}
else
{
$fa = fopen($counterfile, 'w');
fwrite( $fa, 0, 100);
fclose($fa);
chmod($counterfile, 0777);

$counter = "0";
}
return($counter);
}

print Counter();
?>
</BODY>

...and an example of a link on my site is...

<a href="downloads/NK39XSetup.exe" class="linksheet">Download Note
Keeper 3 9X Edition</a>

...just a normal hyperlink obviously :)

any help will be great, thanks :)

(maybe one day ill be good enough to help others)

User avatar
swirlee
Moderator
Moderator
Posts: 2272
Joined: Sat Jul 05, 2003 1:18 pm
Location: A bunk in the back
Contact:

Postby swirlee » Sat Jul 26, 2003 12:57 pm

Change your download link to send the user to a PHP script with a name of your choosing. In this script, put the following code:

<?
/*
insert your counter code here
*/

// now use header() to send the browser to the download
header('Location: downloads/NK39XSetup.exe');
?>

This way, your counter is only updated when the user initiates the download, and the user never notices.

User avatar
TrickyRic
New php-forum User
New php-forum User
Posts: 50
Joined: Mon Mar 03, 2003 6:28 am
Location: Toronto, Canada
Contact:

thanks

Postby TrickyRic » Sat Jul 26, 2003 2:51 pm

thanks, its just urning midnight so i dont have access to the server at the mo but ive written a test page ready and will let you know how it goes in the morning :)

User avatar
TrickyRic
New php-forum User
New php-forum User
Posts: 50
Joined: Mon Mar 03, 2003 6:28 am
Location: Toronto, Canada
Contact:

Postby TrickyRic » Sun Jul 27, 2003 3:58 am

hi again, that idea seems to have worked but for some reason the numbers stopped incrementing, i cant see anything wrong with it and ive been trying for about 3 hours now but still getting nowhere, any help would be even greater, if i change the dat file manually, it updates on the pages so i know the problem lies somewhere on the 'writing back to the file' stage.

heres the first page, with the download link...

<HTML>
<HEAD>
<TITLE>Untitled</TITLE>
<META NAME="GENERATOR" CONTENT="HTMLIATE">
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
</HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000">

<a href="nk3.php" class="linksheet">Download Note Keeper 3 9X Edition</a>
<?php
function Counter($counterfile='counter.dat')
{
if (file_exists($counterfile))
{
$fp = fopen($counterfile, 'r');
$num = fread($fp, 100);
fclose($fp);
$counter = $num;
}
else
{
$fa = fopen($counterfile, 'w');
fwrite( $fa, 0, 100);
fclose($fa);
chmod($counterfile, 0777);

$counter = "0";
}
return($counter);
}
print Counter();
?>
</BODY>
</HTML>

...that page seems to work fine, the number appears next to the link and if i manually edit the file, the number changes to match, so i see no problems there, now, this is the page the link points to...

<?php
function Counter($counterfile='counter.dat')
{
if (file_exists($counterfile))
{
$fp = fopen($counterfile, 'r');
$num = fread($fp, 100);
$cnum = ($num + 1);
fclose($fp);

$fa = fopen($counterfile, 'w');
fwrite( $fa, $cnum, 100);
fclose($fa);
}
else
{
$fa = fopen($counterfile, 'w');
fwrite( $fa, 0, 100);
fclose($fa);
chmod($counterfile, 0777);
}
}
header('Location: NK3.zip');
?>

...the header lines working as i can download the file but for some reason, its either ignoring the rest, or theres a bug somewhere, hopefully this will be the last help i need for a while (or until my next 'good' idea anyway lol)

thanks.

User avatar
swirlee
Moderator
Moderator
Posts: 2272
Joined: Sat Jul 05, 2003 1:18 pm
Location: A bunk in the back
Contact:

Postby swirlee » Sun Jul 27, 2003 7:54 pm

Have you tried clearing your cache and your history, or changing the download location, or trying it from another computer? My guess is that the counter isn't updating because your browser has cached the file you're trying to download.

User avatar
TrickyRic
New php-forum User
New php-forum User
Posts: 50
Joined: Mon Mar 03, 2003 6:28 am
Location: Toronto, Canada
Contact:

Postby TrickyRic » Mon Jul 28, 2003 3:42 am

hi, thanks for the reply, my settings are configured to empty the cach every time the browser is closed (i dont see much point in wasting space if pages load quick enough :) ) though i have manually emptied cache, cookies, and history files and still no look.

the number is updating if i manually edit the .dat file, however, no matter how many times i click the download link (even if i fully download the file) the code isnt writing to the file, i can manually open the .dat file and the number hasnt changed.

the code worked fine until i split it into seperate pages, have i split it wrong or something?

User avatar
TrickyRic
New php-forum User
New php-forum User
Posts: 50
Joined: Mon Mar 03, 2003 6:28 am
Location: Toronto, Canada
Contact:

Postby TrickyRic » Mon Jul 28, 2003 3:54 am

just ran a few more tests and found that if i remove (well comment out) the header line, i get an error 200 message, this could be an abyss error (the web server im running) or an internet explorer error, any ideas?

also, if i add the html tags..

<HTML>
</HTML>

...a get a 'warning, header information cannot be changed' message

User avatar
swirlee
Moderator
Moderator
Posts: 2272
Joined: Sat Jul 05, 2003 1:18 pm
Location: A bunk in the back
Contact:

Postby swirlee » Mon Jul 28, 2003 6:04 am

200 isn't an error message, it's the HTTP "OK" (e.g. "the request completed successfully") code. I'm not sure why you'd be seeing it in your browser, though. See here for reference. As for adding <html> before the PHP, this is because any header() message (as well as setcookie() and some sessions functions) must be sent before any output (including spaces, newlines, or HTML tags) is sent to the browser. So if you have a header() call, it must be in a block of PHP that begins on the first line of the page, and must be before any output function (echo, print, whathaveyou) in that block.

User avatar
TrickyRic
New php-forum User
New php-forum User
Posts: 50
Joined: Mon Mar 03, 2003 6:28 am
Location: Toronto, Canada
Contact:

Postby TrickyRic » Mon Jul 28, 2003 6:12 am

aha, that explains a few things, ok ill make a few modifications and see what happens, thanks :)

User avatar
TrickyRic
New php-forum User
New php-forum User
Posts: 50
Joined: Mon Mar 03, 2003 6:28 am
Location: Toronto, Canada
Contact:

Postby TrickyRic » Mon Jul 28, 2003 7:05 am

hmm, well ive made a HUGE modification, the page thats supposed to do the incrememting and saving the new value is now cut down to the bare minimum code...

<?php
header('Location: NK3.zip');
$counterfile='counter.dat'

$fp = fopen($counterfile, 'r');
$num = fread($fp, 100);
$cnum = ($num + 1);
fclose($fp);

$fa = fopen($counterfile, 'w');
fwrite( $fa, $cnum, 100);
fclose($fa);
?>

...and now its giving me the error...

Parse error: parse error, unexpected T_VARIABLE in C:\PROGRAM FILES\ABYSS WEB SERVER\htdocs\freefornow\test\nk3.php on line 5

...which is refering to the line...

$fp = fopen($counterfile, 'r');

...strange considering its almost identical to the line that opens for write, yet that doesnt return an error.

its also started to ignore the header, though im hoping that will change when this error is sorted.

im getting tired of saying this but... any ideas? thanks

User avatar
TrickyRic
New php-forum User
New php-forum User
Posts: 50
Joined: Mon Mar 03, 2003 6:28 am
Location: Toronto, Canada
Contact:

Postby TrickyRic » Mon Jul 28, 2003 7:15 am

ah.... solved, fixed, wuhooo!!!

about 5 seconds after i posted the last message, i looked at the code again and hadnt added an ; to line 4 :roll:

well anyway, it works now :) now all i gotto do is add it to my site :) ...in about 20 places :S

anyway, thanks for all the help from everybody :D

User avatar
swirlee
Moderator
Moderator
Posts: 2272
Joined: Sat Jul 05, 2003 1:18 pm
Location: A bunk in the back
Contact:

Postby swirlee » Mon Jul 28, 2003 8:34 am

TrickyRic wrote:well anyway, it works now :) now all i gotto do is add it to my site :) ...in about 20 places :S


Glad you got it working. But I recommend that you put it in a file all by itself and then use include() to include the script everywhere you need it.. That way if you have to modify the script, you won't have to manually propgate the changes to all of those pages.

User avatar
TrickyRic
New php-forum User
New php-forum User
Posts: 50
Joined: Mon Mar 03, 2003 6:28 am
Location: Toronto, Canada
Contact:

Postby TrickyRic » Mon Jul 28, 2003 9:03 am

hi, yea i thought of that but id have to add value settings to each hyperlink then (so the script knows what value to change, show, and what file to download)

User avatar
TrickyRic
New php-forum User
New php-forum User
Posts: 50
Joined: Mon Mar 03, 2003 6:28 am
Location: Toronto, Canada
Contact:

Postby TrickyRic » Tue Jul 29, 2003 4:18 am

just a quick post to say its all in place on the site at last :)
http://freefornow.dyndns.org/freefornow
...not very exciting though... just numbers lol

User avatar
swirlee
Moderator
Moderator
Posts: 2272
Joined: Sat Jul 05, 2003 1:18 pm
Location: A bunk in the back
Contact:

Postby swirlee » Tue Jul 29, 2003 5:37 am

I can't help but notice that you have a separate PHP file for every download (e.g. NK3Setup.php, NK39XSetup.php, etc.). Why not just use one script that allows you to specify the file to be downloaded? It ought to save you a lot of trouble if you ever decide to change things around. Of course, for security's sake, you'd want an "allowed files" list to prevent users from grabbing files they're not supposed to.

User avatar
TrickyRic
New php-forum User
New php-forum User
Posts: 50
Joined: Mon Mar 03, 2003 6:28 am
Location: Toronto, Canada
Contact:

Postby TrickyRic » Tue Jul 29, 2003 6:59 am

yea it was a thought but id have to pass values to the file to set which .dat file to use, which link to download, etc... which the user could see on the status bar when putting te curser over the link, therefore the security of the premium files would be loosened

User avatar
swirlee
Moderator
Moderator
Posts: 2272
Joined: Sat Jul 05, 2003 1:18 pm
Location: A bunk in the back
Contact:

Postby swirlee » Tue Jul 29, 2003 7:44 am

True enough. Though there are plenty of ways to handle this. Storing the md5 hash of the filenames, for example, and calling that instead:

Code: Select all

<?
   // the hash of the requested file
   $req_file_hash = trim($_GET['fileid']);

   // specify the allowed files
   $files = array(
      'file1.exe',
      'file2.exe'
      // and so on..
   );

   // create an array where $key = md5($value)
   $files_hashed = array_map(
      // oh my lord it's a lambda function
      create_function(
         '$filename', // arguments
         'return array(md5($filename) => $filename);' // code
      ),
      $files
   );
   
   unset $files; // because I'm anal
   
   if(array_key_exists($req_file_hash)) {
      /* Your counter code goes here */

      // forward them to the file with the matching hash
      header('Location: ' . $files_hashed[$req_file_hash]);
   } else {
      echo "invalid file, go 'way.\n";
   }
?>


And now, on your download page, you can just do the following to link to the file:

Code: Select all

<a href="download.php?fileid="<?= md5('file1.exe') ?>">File One</a>


And the user will never see the filename (instead they'll see something like download.php?fileid=df1e03187195a69480229675cda613fe). Of course, you'll have to modify your counter code slightly, so it knows which dat file to write to (have you considered modifying the counter code so you can store all of said data in one file? just an idea).

Also, I must apologize for dropping that anonymous ("lambda") function in there. I'd never used them before, and this looked like the perfect opportunity. Anyway, I don't know if you'll use this code or not, but I hope it's helpful -- I learned how to use lambda functions in the process, so it was worthwhile for me. :D

User avatar
TrickyRic
New php-forum User
New php-forum User
Posts: 50
Joined: Mon Mar 03, 2003 6:28 am
Location: Toronto, Canada
Contact:

Postby TrickyRic » Tue Jul 29, 2003 7:52 am

hmm, the code looks good and ill bear it in mind, though ill stick to the current method for now (dont wanna risk more debuggin now it works lol)

User avatar
swirlee
Moderator
Moderator
Posts: 2272
Joined: Sat Jul 05, 2003 1:18 pm
Location: A bunk in the back
Contact:

Postby swirlee » Tue Jul 29, 2003 8:10 am

As it turns out, my code doesn't quite work, as the array_map() function doesn't quite work like I want it to. To fix it, skip the lambda function and just use a for() loop to iterate through the array and generate the md5 key pairs. If you're interested, I can fix it, otherwise, I'll leave it up to someone else.


Return to “PHP Scripts”

Who is online

Users browsing this forum: Yahoo [Bot] and 1 guest

cron