bottom
wait
Valid XHTML 1.0!
Get Firefox!
Programming tutorials - Total number of downloads (PHP)
Let's say that you need to find out how many times a program has been downloaded from your web site. Is quite easy. You need a MySQL data base and PHP.
Let's start!
First you need to make a link to self page and add a http variable called dwn. You set the value as the program name. without extension (for a better view of source code).
<a href="download.php?dwn=program" title="Download program">Download program</a>
When you click the link, the variable $_GET['dwn'] is sending with the value "program". If this variable is set with the value "program" we appeal the javascript function called loading(download).
<body <?php if($_GET['dwn']=='program')
echo ' onload=\'loading("program.exe")\''; ?> >
Once called this function, we take over it parameter and change window's location to parameter's value, after 3 seconds the variable $_GET['dwn'] will unset through reload of page but without http variable.
<script type="text/javascript">
<!--
function loading(download){
window.location.href=download;
setTimeout('window.location.href="download.php"', 3000);
}
//-->
</script>
Beforehand we connect to data base and get the value of last download called $nr. If the variable $_GET['dwn'] is set, we'll increase $nr with one unit getting $nrp variable which we'll use for UPDATE it in data base.
<?php
// Get the number of downloads from data base
$sql = "SELECT program from download";
$query = mysql_query($sql);
$row = mysql_fetch_object($query);
$nr = $row->program;
// If the link is clicked increase the number of downloads and update into data base
if ($_GET['dwn'] == "program"){
$nrp = $nr+1;
$sql = "UPDATE download SET ".$_GET['dwn']." = '".$nrp."' WHERE ".$_GET['dwn']." = '".$nr."'";
$query = mysql_query($sql);
}
?>
Because i've seen on many web sites that near total number of downloads is shown the size of file attended to download, we'll do the same.
// File size
$filename = 'program.exe';
$kb = filesize($filename)/1024;
$kb = floor($kb*100)/100;
All we have to do now is to show the total number of downloads, that have been made, using variable $nr. After 3 seconds since you click the link of download, $nr will actualise with the value from data base.
Below you have the entire source code (php and SQL).
I hope that this tutorial was usefully, Thank you for reading.
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <title>Download</title>
  5. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  6. <?php if($_GET['dwn'] != ''){ ?>
  7. <script type="text/javascript">
  8. <!--
  9. function loading(download){
  10. window.location.href=download;
  11. setTimeout('window.location.href="download.php"', 3000);
  12. }
  13. //-->
  14. </script>
  15. <?php } ?>
  16. </head>
  17. <?php
  18. // File size
  19. $filename = 'program.exe';
  20. $kb = filesize($filename)/1024;
  21. $kb = floor($kb*100)/100;
  22. // Connecting to data base
  23. $con = mysql_connect("localhost","root","");
  24. $select_db = mysql_select_db("test");
  25. ?>
  26. <body <?php if($_GET['dwn']=='program')
  27. echo ' onload=\'loading("program.exe")\''; ?> >
  28. <?php
  29. // Get the number of downloads from data base
  30. $sql = "SELECT program from download";
  31. $query = mysql_query($sql);
  32. $row = mysql_fetch_object($query);
  33. $nr = $row->program;
  34. // If the link is clicked increase the number of downloads and update into data base
  35. if ($_GET['dwn'] == "program"){
  36. $nrp = $nr+1;
  37. $sql = "UPDATE download SET ".$_GET['dwn']." = '".$nrp."' WHERE ".$_GET['dwn']." = '".$nr."'";
  38. $query = mysql_query($sql);
  39. }
  40. ?>
  41. <a href="download.php?dwn=program" title="Download program">Download program</a>
  42. &nbsp;&nbsp;Size: <?php echo $kb.' kB'; ?>&nbsp;&nbsp;Downloaded: <?php echo $nr; ?> times
  43. </body>
  44. </html>
CREATE TABLE `download` (
`program` int(11) NOT NULL default '0'
) TYPE=MyISAM;

INSERT INTO `download` VALUES (0);
Print this page
bottom