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
$sql = "SELECT program from download";
$query = mysql_query($sql);
$row = mysql_fetch_object($query);
$nr = $row->program;
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.
$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.
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <title>Download</title>
- <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
- <?php if($_GET['dwn'] != ''){ ?>
- <script type="text/javascript">
- function loading(download){
- window.location.href=download;
- setTimeout('window.location.href="download.php"', 3000);
- }
- </script>
- <?php } ?>
- </head>
- <?php
- $filename = 'program.exe';
- $kb = filesize($filename)/1024;
- $kb = floor($kb*100)/100;
- $con = mysql_connect("localhost","root","");
- $select_db = mysql_select_db("test");
- ?>
- <body <?php if($_GET['dwn']=='program')
- echo ' onload=\'loading("program.exe")\''; ?> >
- <?php
- $sql = "SELECT program from download";
- $query = mysql_query($sql);
- $row = mysql_fetch_object($query);
- $nr = $row->program;
- if ($_GET['dwn'] == "program"){
- $nrp = $nr+1;
- $sql = "UPDATE download SET ".$_GET['dwn']." = '".$nrp."' WHERE ".$_GET['dwn']." = '".$nr."'";
- $query = mysql_query($sql);
- }
- ?>
- <a href="download.php?dwn=program" title="Download program">Download program</a>
- Size: <?php echo $kb.' kB'; ?> Downloaded: <?php echo $nr; ?> times
- </body>
- </html>
CREATE TABLE `download` (
`program` int(11) NOT NULL default '0'
) TYPE=MyISAM;
INSERT INTO `download` VALUES (0); |