Download Functioality

Hi All

I am developing a shopping cart solution which will allow users to by some music online. Once the user successfully complets the payment he should be able to download an MP3 file. Any suggestion how can implement this functionality.

Basically It should be as follows
1.) Only the user who paid for it only able to download it.
2.) The download link corresponding to the user should expire after some time. But i want to keep the file in the same path as other users will have a download link for it.

Any idea about how rapidshare and all works?

Thanks in advance.
Ben

This won’t work, but should give a rough idea of how to do this. :slight_smile:


<?php
$id = isset($_GET['transaction_id']) ? (int)$_GET['transaction_id'] : 0 ;

if(0 === $id){
  exit;
}

$sql = sprintf(
  "SELECT
    transaction.id,
    track.path
  FROM
    transaction
  LEFT JOIN
    track ON track.id = transaction.track_id
  WHERE
    transaction.id = %d AND transaction.expires > CURRENT_DATE
  LIMIT 1;",
  $id
);

$res = mysql_query($sql);

if(1 !== mysql_num_rows($res)){
  exit;
}

$track = mysql_fetch_assoc($res);

header('Content-Type: audio/mpeg');
readfile($track['path']);
exit;

Anthony provided a great example here, however I just have to point out that the operation of serving files trough PHP might be slow for larger volume website.

Hi Anthony

Thanks for your help. It sounds like a good idea. I will try this now and let you know.

Many Thanks
Ben