Board index   FAQ   Search  
Register  Login
Board index php forum :: php coding PHP coding => General

Counting the number of times an MP3 is downloaded...?

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

Counting the number of times an MP3 is downloaded...?

Postby eyewoo » Tue Mar 18, 2003 4:07 am

I have a slew of MP3's of my audio work on my website. Each is available using an HTML <a href="finm.mp3">click</a> link. How can PHP know when a link has been used so that I can increment a $counter variable? ....or is there a better way other than using the HTML <a href="finm.mp3">click</a> link to make files available for download?
eyewoo
New php-forum User
New php-forum User
 
Posts: 71
Joined: Sun Mar 09, 2003 10:29 am
Location: Pennsylvania, USA

Postby Redcircle » Tue Mar 18, 2003 9:49 am

What I would do is link to a php script which updates the counter then it redirects to the file.

here's the link. <a href="file.php?file=http://yourserver.com/mp3/finm.mp3">click</a>

here's seudo code.
<?php

get var from uri.

increment counter file.

redirect to file

?>
User avatar
Redcircle
Moderator
Moderator
 
Posts: 830
Joined: Tue Jan 21, 2003 10:42 pm
Location: Michigan USA

Postby eyewoo » Tue Mar 18, 2003 10:01 am

OK... thanks... I think I understand that... 8)

I'll give it a try.
eyewoo
New php-forum User
New php-forum User
 
Posts: 71
Joined: Sun Mar 09, 2003 10:29 am
Location: Pennsylvania, USA

Postby eyewoo » Tue Mar 18, 2003 12:31 pm

Redcircle...

I'm stuck on the redirecting part. In your example, you send the download url for the MP3 in the variable $file. What I don't understand how to do is to actually kick off the download once variable $file is in the code file that updates the counter.

It would be simple if it were something like get($file).

Assuming $file holds "http://yourserver.com/mp3/finm.mp3", what is the function or line of code that would start the download...?
eyewoo
New php-forum User
New php-forum User
 
Posts: 71
Joined: Sun Mar 09, 2003 10:29 am
Location: Pennsylvania, USA

Postby Xerpher » Tue Mar 18, 2003 1:23 pm

I already posted download code here, hope it helps :)
Xerpher
New php-forum User
New php-forum User
 
Posts: 164
Joined: Tue Aug 27, 2002 8:25 pm
Location: Ontario, Canada

Postby eyewoo » Tue Mar 18, 2003 1:46 pm

Xerpher... did not help because I'm not really sure what your doing there.

Security is not a problem here. Here's what needs to be done. I have a list of links to MP3 files. Click on a link and the MP3 file starts downloading to the user's computer. I want to be able to count how many times each MP3 is clicked on. Redcircle suggested that the actual link should send the MP3's URL to a interim php file that does the counting before the download begins. My question is how to initiate the download in that interim file without setting up another link to click on....?
eyewoo
New php-forum User
New php-forum User
 
Posts: 71
Joined: Sun Mar 09, 2003 10:29 am
Location: Pennsylvania, USA

Postby Xerpher » Tue Mar 18, 2003 1:56 pm

You can initiate the download by taking the code I posted before and removing the if security statement :P

Code: Select all
<?php
$dir = '/path/to/new/directory/';
header("Content-type: application/octet-stream");
header("Content-length: ".filesize(stripslashes($dir.$_GET['filename'])));
readfile(stripslashes($dir.$_GET['filename']));
?>

This method will allow you to get files from anywhere in your server. An alternate method that works the same that is shorter so you could stick to this one is:

Code: Select all
<?php
$dir = 'http://www.eyewoo.com/path/to/mp3s/';
header("Location: ".$dir.$_GET['filename']);
?>
Xerpher
New php-forum User
New php-forum User
 
Posts: 164
Joined: Tue Aug 27, 2002 8:25 pm
Location: Ontario, Canada

Postby eyewoo » Tue Mar 18, 2003 2:12 pm

OK... now I see it... Thanks... 8)
eyewoo
New php-forum User
New php-forum User
 
Posts: 71
Joined: Sun Mar 09, 2003 10:29 am
Location: Pennsylvania, USA

Postby eyewoo » Tue Mar 18, 2003 3:06 pm

That works great...

...but... :) ... I'm curious... is there a way to accomplish the same thing, but with something similar to the "header" statement within the body of the HTML?
eyewoo
New php-forum User
New php-forum User
 
Posts: 71
Joined: Sun Mar 09, 2003 10:29 am
Location: Pennsylvania, USA

Postby Xerpher » Tue Mar 18, 2003 3:32 pm

I can't remember if theres a delay you can put on the location header, but I'm quite sure that if you ptu the header at the top and then html code after it, all of it should appear... if thats what you mean. But then, if you want to show a page AND download a file, I think the location header might stop loading the page, so you might want to use the first set of code I showed you.
Xerpher
New php-forum User
New php-forum User
 
Posts: 164
Joined: Tue Aug 27, 2002 8:25 pm
Location: Ontario, Canada

Postby eyewoo » Tue Mar 18, 2003 4:05 pm

Thanks once again for your help... 8)
eyewoo
New php-forum User
New php-forum User
 
Posts: 71
Joined: Sun Mar 09, 2003 10:29 am
Location: Pennsylvania, USA

Postby eyewoo » Tue Mar 18, 2003 4:21 pm

oops... I thought this was figured out, but there is a serious problem which I'll explain after showing the two files that I'm using for testing:

Here's the opening file:
==================start
<html>
<head>
<title>test</title>
</head>
<body topmargin=24 leftmargin=24 marginheight=24 marginwidth=24>

<a href="getmp3.php?download=http://www.mojoko.com/test/php/variations.low.mp3">clicky</a>

</body>
</html>
===================end

And here's the called file: getmp3.php

==================start
<? header("Location: ".$download.$_GET['filename']); ?>
<html>
<head>
<title>mp3</title>
</head>
<body topmargin=24 leftmargin=24 marginheight=24 marginwidth=24>

<?php echo "Hello<hr>"; ?>

</body>
</html>
===================end

It works fine for downloading the mp3 file, however in the getmp3.php file there is a line of code in the body that simply echos the word "Hello". That word never happens, so I can presume that any code to fetch, increment and store a count variable will also not happen. But that was the point...

Any suggestions...?
eyewoo
New php-forum User
New php-forum User
 
Posts: 71
Joined: Sun Mar 09, 2003 10:29 am
Location: Pennsylvania, USA

Postby Xerpher » Tue Mar 18, 2003 5:05 pm

Xerpher wrote:I can't remember if theres a delay you can put on the location header, but I'm quite sure that if you put the header at the top and then html code after it, all of it should appear... if thats what you mean. But then, if you want to show a page AND download a file, I think the location header might stop loading the page, so you might want to use the first set of code I showed you.
Xerpher
New php-forum User
New php-forum User
 
Posts: 164
Joined: Tue Aug 27, 2002 8:25 pm
Location: Ontario, Canada

Postby eyewoo » Tue Mar 18, 2003 5:23 pm

You mean the first set of code like this, which is called by the same first file:
========================start
<html>
<head>
<title>mp3</title>
</head>
<body topmargin=24 leftmargin=24 marginheight=24 marginwidth=24>

<?php
$dir = $download;
header("Content-type: application/octet-stream");
header("Content-length: ".filesize(stripslashes($dir.$_GET['filename'])));
readfile(stripslashes($dir.$_GET['filename']));

echo "Hello<hr>";
?>

</body>
</html>
=============================end

If so, it also does not work. Here's a link if you'd like to run it and see the warnings.

http://www.mojoko.com/test/php
eyewoo
New php-forum User
New php-forum User
 
Posts: 71
Joined: Sun Mar 09, 2003 10:29 am
Location: Pennsylvania, USA

Postby Xerpher » Tue Mar 18, 2003 6:02 pm

No no no...

Code: Select all
<?php
$dir = $download;
header("Content-type: application/force-download");
header("Content-Disposition: attachment; filename=".stripslashes($_GET['filename']));
header("Content-length: ".filesize(stripslashes($dir.$_GET['filename'])));
readfile(stripslashes($dir.$_GET['filename']));
?>
<html>
<head>
<title>mp3</title>
</head>
<body topmargin=24 leftmargin=24 marginheight=24 marginwidth=24>
<?php
echo "Hello<hr>";
?>
</body>
</html>


But I'm pretty sure there needs to be one more header to make sure the download doesn't download the html in to the file either but I can't remember it, so try it and see if it works.

* Always remember, headers come first!
Xerpher
New php-forum User
New php-forum User
 
Posts: 164
Joined: Tue Aug 27, 2002 8:25 pm
Location: Ontario, Canada

Postby eyewoo » Tue Mar 18, 2003 6:04 pm

Xerpher... doesn't work...

Well, maybe this has been the wrong path to follow. It's pretty simple what I want to do -- count the number of times an mp3 file is downloaded.

I'm also just about a week into using php, so I'm definitely a newbie to how it works... How would you do it - count file downloads?
eyewoo
New php-forum User
New php-forum User
 
Posts: 71
Joined: Sun Mar 09, 2003 10:29 am
Location: Pennsylvania, USA

Postby Xerpher » Tue Mar 18, 2003 8:05 pm

I think the best thing for you to do is use a database for your counter :P and use a meta tag for the download to start.
Xerpher
New php-forum User
New php-forum User
 
Posts: 164
Joined: Tue Aug 27, 2002 8:25 pm
Location: Ontario, Canada

Postby Redcircle » Tue Mar 18, 2003 10:41 pm

allrighty, eyewoo.. have you messed with databases yet. That would be the easiest way to do a counting script. BUT I will give an example with a flat file.

My example will include 3 files.

getmp3.php // this is the file that reads in the download.count file and increment it.
mp3file.mp3 // this is the file you are trying to download.
download.count // this is the log file that keeps track of the downloads.
Code: Select all
<?php
// filename: getmp3.php
function read_in_file($counter_file)
{
  $newFile = file($counter_file); //this will read the contents of the file and put each line into an array
  $size = sizeof($newFile);
   for($x; $x<$size; $x++)
   {
     $file_line = explode(',',$newFile[$x]); //makes an array from each line using a , as a delimiter.
     $filelist[$file_line[0]] = $file_line[1]; //creates an associative array from the contents of $file_line
     
   }
return $filelist;
}

function increment_count($filelist, $file)
{
$filelist[$file] = $filelist[$file] + 1;
return $filelist;
}

function write_out_file($counter_file, $filelist)
{
 
   $outFile = @fopen($counter_file, 'w'); //opens file for output
   foreach($filelist as $key=>$value) //writes each file back to file
   {
     fputs($configFile, $key.','.$value."\n" , strlen($key.','.$value."\n"));

   }
   fclose($outFile); //closes file after done writing

}
function get_download_count($filelist, $file) //$filelist is an array that is read using the read_in_file function and $file is the string you are looking for
{
  $count = $filelist[$file];
   return $count;
}

if(isset($_GET['file']))
{
  $list = read_in_file('download.count'); //reads in the file
  $list = increment_count($list, $_GET['file']); //increments the file count
  write_out_file('download.count', $list); //writes file back out
  header('Location: '.$_GET['file']); //changes the location to the actual file they are going to download.
}


within the download.count file it would look like this
Code: Select all
http://www.youthost.com/mp3/mp3file1.mp3,121
http://www.youthost.com/mp3/mp3file2.mp3,321
http://www.youthost.com/mp3/mp3file3.mp3,131


I have not tested this code but it to the best of my knowledge it should work. there may be mispellings and missed characters.. but hey.. I'm not gettin paid right.

Hope this helps a lil
User avatar
Redcircle
Moderator
Moderator
 
Posts: 830
Joined: Tue Jan 21, 2003 10:42 pm
Location: Michigan USA

Postby eyewoo » Wed Mar 19, 2003 3:43 am

Thanks Redcircle... :D :D :D
eyewoo
New php-forum User
New php-forum User
 
Posts: 71
Joined: Sun Mar 09, 2003 10:29 am
Location: Pennsylvania, USA

Postby eyewoo » Wed Mar 19, 2003 10:04 am

Redcircle... Couldn't get it to work... One of the problems has always been the header line:

header('Location: '.$_GET['file']); //changes the location to the actual file

If that line is not the first line in the code file, above the <html> tag, it generates a warning error. The catch-22 is, if its at the top, nothing in the body of the HTML gets run...

...but anyway... I figured out how to do it YEAH!!! -- like so:

Here's the initial file with the link:
<html>
<head>
<title>Link file</title>
</head>
<body topmargin=24 leftmargin=24 marginheight=24 marginwidth=24>

<a href="getit.php">clicky</a>

</body>
</html>


and here's the linked file:

<html>
<head>
<title> Get file and inc counter</title>
<META http-equiv="Refresh" content="1;URL=http://www.mojoko.com/test/php/mp3file.mp3">
</head>
<body>

<?
$fiz = "count.txt";
$px = fopen($fiz,"a+");
rewind($px);
$incCt = fread($px, filesize($fiz));
fclose($px);
if ($incCt == '') {$incCt = "1";} else {$incCt++;}

$px = fopen($fiz,"w");
fwrite($px,"$incCt");
fclose($px);
echo $incCt.".";
?>

</body>
</html>


It works perfectly.... :D

The META tag has a 1 second delay before calling the MP3. That's plenty of time to retrieve the current count, increment it and save it back to disk. In fact, I ran a 1 second test, repeating the file load/file save 50 times and there was plenty of time left over, so doing it just once should work flawlessly.
eyewoo
New php-forum User
New php-forum User
 
Posts: 71
Joined: Sun Mar 09, 2003 10:29 am
Location: Pennsylvania, USA


Return to PHP coding => General

Who is online

Users browsing this forum: No registered users and 1 guest

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.

cron