Vise antal links-klik, der er gemt i MySQL
jeg har fundet dette script, der både tvinger browseren til at downloade den fil, der gemmer sig bag et link (via en Åbn eller Gem Som-dialogboks), samt tæller antal gange, der er blevet klikket på det givne link. Det gemmes i en MySQL-database. Det fungerer helt fint. Jeg ville dog gerne vise det antal gange, der er blevet klikket/downloadet ved siden af linket, i stedet for at skulle kigge i MySQL-databasen hver gang. Er der nogen der kan fortælle mig hvordan det gøres?Her er scriptet som det ser ud nu:
<?
/*
- Save this php-file as download.php.
- Create the 'files' table with the following query
- Modify the $path variable and set the real path where you store all you files
linux eg. '/data/www/my_files/'
win32 eg. 'C:\www\my_files\\' (note the double \ at the end)
you can also use a relative path...
- Add download.php to the file you want to log :
http://www.site.com/download.php?file=FILENAME.ext
- Check your 'files' tables to see the amount of download for each files //Det vil jeg helst slippe for!
### Create table query
CREATE TABLE `downloads` (
`ID` tinyint(4) NOT NULL auto_increment,
`name` varchar(255) NOT NULL default '0',
`downloads` tinyint(4) NOT NULL default '0',
`date` datetime NOT NULL,
PRIMARY KEY (`ID`),
KEY `ID_2` (`ID`)
) TYPE=MyISAM;
*/
### Script configuration
// Database authorization
$conf['host'] = 'localhost';
$conf['base'] = '*****';
$conf['login'] = '*****';
$conf['password'] = '*****';
// Path of the files;
$path = "opgaver/";
function download($path,$file){
$length = filesize("$path/$file");
header("Content-Type: application/force-download; name=\"$file\"");
header("Content-Transfer-Encoding: binary");
header("Content-Length: $length");
header("Content-Disposition: attachment; filename=\"$file\"");
header("Expires: 0");
header("Cache-Control: no-cache, must-revalidate");
header("Pragma: no-cache");
readfile("$path/$file");
}
function err_msg($text) {
$out = '<center>'."\n";
$out .= '<br><br><br>'."\n";
$out .= '<font face="Arial, Helvetica" color="#D20000" size="2"><b>'.$text.'</b></font>'."\n";
$out .= '</center>';
die($out);
}
$file = ( isset($_GET['file']) && $_GET['file']!='' ) ? $_GET['file'] : null;
$request = "select * FROM downloads WHERE name LIKE '$file'";
if(!$file){
err_msg('Specificér en fil <br>('.$_SERVER['PHP_SELF'].'?file=myfile)');
}elseif(!is_file($path.$file)){
err_msg('Fil ikke tilgængelig!');
}
$date = date("Y-m-d H:i:s");
### Is the file already in the db ?
$db = mysql_connect($conf['host'], $conf['login'], $conf['password']);
mysql_select_db($conf['base'],$db);
$req = mysql_query($request);
$in_db = @mysql_num_rows($req);
### The file has not been downloaded yet
if(!$in_db){
$sql = "INSERT INTO downloads (ID, name, downloads, date) Values ('', '$file', '1', '$date')";
### Already downloaded
}else{
$records = mysql_fetch_array($req);
$rec_download = ($records['downloads']+1);
$sql = "UPDATE downloads SET downloads='$rec_download', date='$date' WHERE name='$file'";
}
$db = mysql_connect($conf['host'], $conf['login'], $conf['password']);
mysql_select_db($conf['base'],$db);
$result = mysql_query($sql);
### Send file
download($path,$file);
mysql_close();
?>
