Home > PHP >
Download Counting with Apache and PHP | Sitemap Search |
|
Sections Membership Features
Recent comments
very difficult by alfin Taking the credit for another persons work ? by curious dude. |
Download Counting with Apache and PHPPosted by martin on 1 Jun 2002, last updated on 6 Aug 2002. Collect statistics on the popularity of your downloads with Apache's mod_rewrite and PHP. How some of the others do itSome sites present you an URI like
While the method mentioned above is easy to implement it is not the best way to do it. We want visitors to see the real filename as the URI not as a query string. So what we'll do internally is exactly the same as in the above example but this time the visitor will see the real filename. How to do we do itOur URIs will be in the form of
What do we need
Apache's configurationOptions +FollowSymLinks RewriteEngine On RewriteBase /foobar/ RewriteRule download/send.php - [L] RewriteRule download/(.+\..+)$ download/send.php?file=$1 [L] You can put this block of code in a What we do is switch on The next two lines are our rewrite rules, if the request is for
The download counter
<?php
$file = isset($_GET['file']) ? trim($_GET['file']) : '';
if (!$file) {
die("Error");
}
if ( substr_count($file, '..') > 0 or substr($file, 0, 1) == '/' ) {
die("Invalid filename.");
}
$path = dirname($_SERVER['PATH_TRANSLATED']) . '/' . $file;
if ( !file_exists($path) ) {
die("File not found: $file");
}
$ext = explode('.', $file);
if ( sizeof($ext) < 2 ) {
die("Invalid filename: should have extension");
}
We do some checking first, you should never
display files to the visitors that they have requested without checking
for unwanted characters like The
$ext = $ext[sizeof($ext)-1];
switch ($ext) {
case 'tgz' :
$type = 'application/x-gzip';
break;
case 'php' :
$type = 'text/html';
break;
default :
$type = 'text/plain';
}
header("Content-type: $type");
By default PHP sends a content type header of Because we send a HTTP header there should be nothing sent to the browser before the last line of the block above. Output buffering can be used as a way around this but it's not needed here.
$fd = fopen ($path, "r");
$code = fread ($fd, filesize($path));
fclose ($fd);
switch ($ext) {
case 'php' :
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/1999/REC-html401-19991224/loose.dtd">
<html>
<head>
<title><?php echo $file?> syntax highlighted</title>
</head>
<body>
<?php
highlight_string($code);
?>
</body>
</html>
<?php
break;
default :
echo $code;
}
This is the part which sends the file to the browser or presents the highlighted PHP file. The functions used are binary safe so you can send every type of files not only text.
require_once('../../config.php');
$db = db_connect();
$sql = "UPDATE download_file SET count=count+1 WHERE file = '$file' ";
$db->query($sql);
?>
And the final one, which actually counts the download. Include a file
with our database info first - as you can see if opens a file which is
out of the webserver root which makes it pretty safe. We connect to
the database with our predefined function The query updated the Note: Never make the mistake to first Commentspoll chat webboard by nut (krajangduang@chaiyo.com) on 13 Feb 2003 8:13am GMT i want poll,chat,webboard(php) Useful addition to database operation by Gokhan (gokhan@mira-soft.com) on 21 Mar 2003 11:51am GMT I've added a few code. It controls and inserts new row if file name not exist in table. If exist increments count value. Hope it helps some one. I will also implement get additional info about visitor too. Code snippet ------------------ $addrecord ="INSERT INTO download_file (count_id, count, file) VALUES (NULL, 1, '$file')"; $check ="SELECT file, counter FROM download_file WHERE file= '$file'"; $update_it = "UPDATE download_file SET counter=counter+1 WHERE file = '$file' "; $result = my_own_mysql_query_routine($check); if (mysql_num_rows($result)) { my_own_mysql_query_routine($update_it); } else { my_own_mysql_query_routine($addrecord); } ------------------- end snippet Take care Use INSERT IGNORE by martin on 22 Mar 2003 1:20pm GMT I think it would be faster like: INSERT IGNORE INTO download_file ... UPDATE download_file SET ... It doesn't involve 2 way communication with the database and a check in between. ok by () on 1 May 2003 4:55pm GMT good work Windows and binary files by martin on 21 Jun 2003 4:24pm GMT A small note, on Windows use $fd = fopen ($path, "rb"); to open the files in binary mode. Note the added b. php by sriramakrishna darla (ramkidarla@hotmail.com) on 22 Feb 2004 6:29am GMT download php files Mongol by Jargalsaihan (bjargalsaihan@chinggis.com) on 28 Apr 2004 3:47am GMT Chi tegeed yax geed baigaa um |