473,231 Members | 1,790 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,231 software developers and data experts.

PHP file download counter

I use htaccess to protect directory and granting access to download
file only for the authorized users. Just want implement simple PHP file
download counter for single file. I need track the number of downloads
of this file on my website, IP address and date. Since I have no access
to Apache log files, I need some other way, use script that will write
a log file. Is there some handy way?

M.

Oct 23 '06 #1
7 3315

"mistral" <po*******@softhome.netwrote in message
news:11*********************@e3g2000cwe.googlegrou ps.com...
I use htaccess to protect directory and granting access to download
file only for the authorized users. Just want implement simple PHP file
download counter for single file. I need track the number of downloads
of this file on my website, IP address and date. Since I have no access
to Apache log files, I need some other way, use script that will write
a log file. Is there some handy way?

M.
Write a script that supplies the file download with the correct mime type
and in that script record the details you want to the place you want...
Oct 23 '06 #2

"Johnny писал(а):
"
"mistral" <po*******@softhome.netwrote in message
news:11*********************@e3g2000cwe.googlegrou ps.com...
I use htaccess to protect directory and granting access to download
file only for the authorized users. Just want implement simple PHP file
download counter for single file. I need track the number of downloads
of this file on my website, IP address and date. Since I have no access
to Apache log files, I need some other way, use script that will write
a log file. Is there some handy way?
M.
-----------
Write a script that supplies the file download with the correct mime type
and in that script record the details you want to the place you want...
-----------------

How to interface this with htaccess? I am new in Php, perhaps there is
ready scripts written for this tasks?

M.

Oct 23 '06 #3
Johnny is right. Here's some code I've used before that does something
similar. This will log the request to a flat file then redirect the
user:

<?

//
// Class SimpleRedirectLog
//
// Create a simple redirect log. Sample usage:
//
// SimpleRedirectLog::logRedirect('file.zip');
//
// That will update a file called "logs/file.zip-access-log.csv"
// with the IP address of the client and the number of times the
// URL has been accessed. The client will be redirected to "file.zip".
//

class SimpleRedirectLog
{
function logRedirect($url, $logPath = null)
{
if (SimpleRedirectLog::logAccess($url, $logPath) !== false) {
header("Location: $url");
return true;
}
return false;
}

function logAccess($url, $logPath = null)
{
$ok = false;
$counter = 0;

if (is_null($logPath)) {
$logPath = dirname(__FILE__) . '/logs/' .
preg_replace('#[/\\\]#', '_', $url) .
'-access-log.csv';
}

if (!($fh = fopen($logPath, 'a+'))) {
trigger_error('Failed to open log file.');
} elseif (!flock($fh, LOCK_EX)) {
trigger_error('Failed to lock log file.');
} elseif (($size = filesize($logPath)) === false) {
trigger_error('Failed to get log file size.');
} else {
$err = false;
if ($size 0) {
// Get the current counter value
$val = '';
$offset = -1;
for (;;) {
if (fseek($fh, $offset--, SEEK_END) !== 0) {
trigger_error("Failed to retrieve counter.");
$err = true;
} elseif (($c = fgetc($fh)) === false) {
break;
} elseif ($c == ',') {
if (!preg_match('/^[1-9][0-9]*$/', $val)) {
trigger_error("Stored counter is invalid: \"$val\".");
$err = true;
} else {
$counter = intval($val);
}
break;
} elseif ($c != "\r" && $c != "\n") {
$val = $c . $val;
}
}
if (fseek($fh, 0, SEEK_END) !== 0) {
trigger_error("Failed to seek in log file.");
$err = true;
}
}
if (!$err) {
$entry =
SimpleRedirectLog::csvEncode(date('Y/m/d G:i:s')) . ',' .
SimpleRedirectLog::csvEncode($url) . ',' .
SimpleRedirectLog::csvEncode($_SERVER['REMOTE_ADDR']) . ',' .
($counter + 1) . "\n";
if (!fwrite($fh, $entry)) {
trigger_error('Failed to write log data.');
} else {
$ok = true;
}
}
}

if ($fh && !fclose($fh)) {
trigger_error('Failed to close log file.');
$ok = false;
}

return $ok ? $counter : false;
}

function csvEncode($str)
{
if (preg_match("/[,\"\r\n]/", $str)) {
$str = '"' . preg_replace('/([" \\\\])/', '\\\\\1', $str) . '"';
}
return $str;
}
}

?>

You could modify this to use readfile() if you want to send the file
directly from PHP instead of using a redirect.

mistral wrote:
"Johnny писал(а):
"
"mistral" <po*******@softhome.netwrote in message
news:11*********************@e3g2000cwe.googlegrou ps.com...
I use htaccess to protect directory and granting access to download
file only for the authorized users. Just want implement simple PHP file
download counter for single file. I need track the number of downloads
of this file on my website, IP address and date. Since I have no access
to Apache log files, I need some other way, use script that will write
a log file. Is there some handy way?
M.
-----------
Write a script that supplies the file download with the correct mime type
and in that script record the details you want to the place you want...

-----------------

How to interface this with htaccess? I am new in Php, perhaps there is
ready scripts written for this tasks?

M.
Oct 24 '06 #4

"pe*******@gmail.com wrote:
"
Johnny is right. Here's some code I've used before that does something
similar. This will log the request to a flat file then redirect the
user:
<?
//
// Class SimpleRedirectLog
//
// Create a simple redirect log. Sample usage:
//
// SimpleRedirectLog::logRedirect('file.zip');
//
// That will update a file called "logs/file.zip-access-log.csv"
// with the IP address of the client and the number of times the
// URL has been accessed. The client will be redirected to "file.zip".
//

class SimpleRedirectLog
{
function logRedirect($url, $logPath = null)
{
if (SimpleRedirectLog::logAccess($url, $logPath) !== false) {
header("Location: $url");
return true;
}
return false;
}

function logAccess($url, $logPath = null)
{
$ok = false;
$counter = 0;

if (is_null($logPath)) {
$logPath = dirname(__FILE__) . '/logs/' .
preg_replace('#[/\\\]#', '_', $url) .
'-access-log.csv';
}

if (!($fh = fopen($logPath, 'a+'))) {
trigger_error('Failed to open log file.');
} elseif (!flock($fh, LOCK_EX)) {
trigger_error('Failed to lock log file.');
} elseif (($size = filesize($logPath)) === false) {
trigger_error('Failed to get log file size.');
} else {
$err = false;
if ($size 0) {
// Get the current counter value
$val = '';
$offset = -1;
for (;;) {
if (fseek($fh, $offset--, SEEK_END) !== 0) {
trigger_error("Failed to retrieve counter.");
$err = true;
} elseif (($c = fgetc($fh)) === false) {
break;
} elseif ($c == ',') {
if (!preg_match('/^[1-9][0-9]*$/', $val)) {
trigger_error("Stored counter is invalid: \"$val\".");
$err = true;
} else {
$counter = intval($val);
}
break;
} elseif ($c != "\r" && $c != "\n") {
$val = $c . $val;
}
}
if (fseek($fh, 0, SEEK_END) !== 0) {
trigger_error("Failed to seek in log file.");
$err = true;
}
}
if (!$err) {
$entry =
SimpleRedirectLog::csvEncode(date('Y/m/d G:i:s')) . ',' .
SimpleRedirectLog::csvEncode($url) . ',' .
SimpleRedirectLog::csvEncode($_SERVER['REMOTE_ADDR']) . ',' .
($counter + 1) . "\n";
if (!fwrite($fh, $entry)) {
trigger_error('Failed to write log data.');
} else {
$ok = true;
}
}
}

if ($fh && !fclose($fh)) {
trigger_error('Failed to close log file.');
$ok = false;
}

return $ok ? $counter : false;
}

function csvEncode($str)
{
if (preg_match("/[,\"\r\n]/", $str)) {
$str = '"' . preg_replace('/([" \\\\])/', '\\\\\1', $str) . '"';
}
return $str;
}
}

?>

You could modify this to use readfile() if you want to send the file
directly from PHP instead of using a redirect.

mistral wrote:
"Johnny писал(а):
"
"mistral" <po*******@softhome.netwrote in message
news:11*********************@e3g2000cwe.googlegrou ps.com...
I use htaccess to protect directory and granting access to download
file only for the authorized users. Just want implement simple PHP file
download counter for single file. I need track the number of downloads
of this file on my website, IP address and date. Since I have no access
to Apache log files, I need some other way, use script that will write
a log file. Is there some handy way?
M.
-----------
Write a script that supplies the file download with the correct mime type
and in that script record the details you want to the place you want...
-----------------
How to interface this with htaccess? I am new in Php, perhaps there is
ready scripts written for this tasks?
M.
------------------

I tried script, but not work for me. What variables need be edited?

M.

Oct 24 '06 #5
Hi,

Try setting error_reporting(E_ALL) and watch for any errors. Pls paste
your script that uses the class...

By default you'll need a web-writable directory called "logs" located
in the same directory where the class file is.

mistral wrote:
"pe*******@gmail.com wrote:
"
Johnny is right. Here's some code I've used before that does something
similar. This will log the request to a flat file then redirect the
user:
<?
//
// Class SimpleRedirectLog
//
// Create a simple redirect log. Sample usage:
//
// SimpleRedirectLog::logRedirect('file.zip');
//
// That will update a file called "logs/file.zip-access-log.csv"
// with the IP address of the client and the number of times the
// URL has been accessed. The client will be redirected to "file.zip".
//

class SimpleRedirectLog
{
function logRedirect($url, $logPath = null)
{
if (SimpleRedirectLog::logAccess($url, $logPath) !== false) {
header("Location: $url");
return true;
}
return false;
}

function logAccess($url, $logPath = null)
{
$ok = false;
$counter = 0;

if (is_null($logPath)) {
$logPath = dirname(__FILE__) . '/logs/' .
preg_replace('#[/\\\]#', '_', $url) .
'-access-log.csv';
}

if (!($fh = fopen($logPath, 'a+'))) {
trigger_error('Failed to open log file.');
} elseif (!flock($fh, LOCK_EX)) {
trigger_error('Failed to lock log file.');
} elseif (($size = filesize($logPath)) === false) {
trigger_error('Failed to get log file size.');
} else {
$err = false;
if ($size 0) {
// Get the current counter value
$val = '';
$offset = -1;
for (;;) {
if (fseek($fh, $offset--, SEEK_END) !== 0) {
trigger_error("Failed to retrieve counter.");
$err = true;
} elseif (($c = fgetc($fh)) === false) {
break;
} elseif ($c == ',') {
if (!preg_match('/^[1-9][0-9]*$/', $val)) {
trigger_error("Stored counter is invalid: \"$val\".");
$err = true;
} else {
$counter = intval($val);
}
break;
} elseif ($c != "\r" && $c != "\n") {
$val = $c . $val;
}
}
if (fseek($fh, 0, SEEK_END) !== 0) {
trigger_error("Failed to seek in log file.");
$err = true;
}
}
if (!$err) {
$entry =
SimpleRedirectLog::csvEncode(date('Y/m/d G:i:s')) . ',' .
SimpleRedirectLog::csvEncode($url) . ',' .
SimpleRedirectLog::csvEncode($_SERVER['REMOTE_ADDR']) . ',' .
($counter + 1) . "\n";
if (!fwrite($fh, $entry)) {
trigger_error('Failed to write log data.');
} else {
$ok = true;
}
}
}

if ($fh && !fclose($fh)) {
trigger_error('Failed to close log file.');
$ok = false;
}

return $ok ? $counter : false;
}

function csvEncode($str)
{
if (preg_match("/[,\"\r\n]/", $str)) {
$str = '"' . preg_replace('/([" \\\\])/', '\\\\\1', $str) . '"';
}
return $str;
}
}

?>

You could modify this to use readfile() if you want to send the file
directly from PHP instead of using a redirect.

mistral wrote:
"Johnny писал(а):
"
"mistral" <po*******@softhome.netwrote in message
news:11*********************@e3g2000cwe.googlegrou ps.com...
I use htaccess to protect directory and granting access to download
file only for the authorized users. Just want implement simple PHP file
download counter for single file. I need track the number of downloads
of this file on my website, IP address and date. Since I have no access
to Apache log files, I need some other way, use script that will write
a log file. Is there some handy way?
M.
-----------
Write a script that supplies the file download with the correct mime type
and in that script record the details you want to the place you want...
-----------------
How to interface this with htaccess? I am new in Php, perhaps there is
ready scripts written for this tasks?
M.

------------------

I tried script, but not work for me. What variables need be edited?

M.
Oct 24 '06 #6
"pe*******@gmail.com wrote:
"
Hi,
Try setting error_reporting(E_ALL) and watch for any errors. Pls paste
your script that uses the class...
By default you'll need a web-writable directory called "logs" located
in the same directory where the class file is.
mistral wrote:
"pe*******@gmail.com wrote:
"
Johnny is right. Here's some code I've used before that does something
similar. This will log the request to a flat file then redirect the
user:
<?
//
// Class SimpleRedirectLog
//
// Create a simple redirect log. Sample usage:
//
// SimpleRedirectLog::logRedirect('file.zip');
//
// That will update a file called "logs/file.zip-access-log.csv"
// with the IP address of the client and the number of times the
// URL has been accessed. The client will be redirected to "file.zip".
//
class SimpleRedirectLog
{
function logRedirect($url, $logPath = null)
{
if (SimpleRedirectLog::logAccess($url, $logPath) !== false) {
header("Location: $url");
return true;
}
return false;
}
function logAccess($url, $logPath = null)
{
$ok = false;
$counter = 0;
if (is_null($logPath)) {
$logPath = dirname(__FILE__) . '/logs/' .
preg_replace('#[/\\\]#', '_', $url) .
'-access-log.csv';
}
if (!($fh = fopen($logPath, 'a+'))) {
trigger_error('Failed to open log file.');
} elseif (!flock($fh, LOCK_EX)) {
trigger_error('Failed to lock log file.');
} elseif (($size = filesize($logPath)) === false) {
trigger_error('Failed to get log file size.');
} else {
$err = false;
if ($size 0) {
// Get the current counter value
$val = '';
$offset = -1;
for (;;) {
if (fseek($fh, $offset--, SEEK_END) !== 0) {
trigger_error("Failed to retrieve counter.");
$err = true;
} elseif (($c = fgetc($fh)) === false) {
break;
} elseif ($c == ',') {
if (!preg_match('/^[1-9][0-9]*$/', $val)) {
trigger_error("Stored counter is invalid: \"$val\".");
$err = true;
} else {
$counter = intval($val);
}
break;
} elseif ($c != "\r" && $c != "\n") {
$val = $c . $val;
}
}
if (fseek($fh, 0, SEEK_END) !== 0) {
trigger_error("Failed to seek in log file.");
$err = true;
}
}
if (!$err) {
$entry =
SimpleRedirectLog::csvEncode(date('Y/m/d G:i:s')) . ',' .
SimpleRedirectLog::csvEncode($url) . ',' .
SimpleRedirectLog::csvEncode($_SERVER['REMOTE_ADDR']) . ',' .

($counter + 1) . "\n";
if (!fwrite($fh, $entry)) {
trigger_error('Failed to write log data.');
} else {
$ok = true;
}
}
}
if ($fh && !fclose($fh)) {
trigger_error('Failed to close log file.');
$ok = false;
}
return $ok ? $counter : false;
}
function csvEncode($str)
{
if (preg_match("/[,\"\r\n]/", $str)) {
$str = '"' . preg_replace('/([" \\\\])/', '\\\\\1', $str) . '"';
}
return $str;
}

}
?>
You could modify this to use readfile() if you want to send the file
directly from PHP instead of using a redirect.
mistral wrote:
"
"mistral" <po*******@softhome.netwrote in message
news:11*********************@e3g2000cwe.googlegrou ps.com...
I use htaccess to protect directory and granting access to download
file only for the authorized users. Just want implement simple PHP file
download counter for single file. I need track the number of downloads
of this file on my website, IP address and date. Since I have no access to Apache log files, I need some other way, use script that will write a log file. Is there some handy way?
M.
-----------
Write a script that supplies the file download with the correct mime type
and in that script record the details you want to the place you want...
-----------------
How to interface this with htaccess? I am new in Php, perhaps there is
ready scripts written for this tasks?
M.
------------------
I tried script, but not work for me. What variables need be edited?
M.
-----------

Hi,

I tried link file to download:

http://www.mydomain.com/SimpleRedire...p?title=MyFile

M.

Oct 25 '06 #7
Hi,

You can add this to the bottom of the script to handle that
URL:

if (isset($_GET['title'])) {
SimpleRedirectLog::logRedirect($_GET['title']);
}
Hi,

I tried link file to download:

http://www.mydomain.com/SimpleRedire...p?title=MyFile

M.
Oct 25 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Bernhard | last post by:
I am not sure if php can achieve this, but i guess that my problem shoulb be solved with an server side language. Is there any way i can tell if a visitor of my website has finished a download?...
0
by: Marius III | last post by:
Hi there, I am building a File download counter in PHP5. It's working fine but the problem is that its not working with any Download Managers for ex: Free Download Manager / DAP etc. The...
1
by: Roy | last post by:
Hi, I have a problem that I have been working with for a while. I need to be able from server side (asp.net) to detect that the file i'm streaming down to the client is saved...
4
by: pradqdo | last post by:
Hi folks, I have a very strange problem when I try to port my client/server program to cygwin. It is a simple shell program where the server executes client's commands + it can send and receive...
39
by: cj | last post by:
I have a 2005 TCP/IP server that creates a new thread to handle each incoming TCP/IP request. Once the request has been answered by the thread the TCP/IP socket is disconnected and the sub/thread...
3
by: malaysiauser | last post by:
Dear all, Last year I install a download control script in Linux server. its working. This year i'd changed my hosting server to other company. i'd tried install the script. It was installed...
13
by: Adhal | last post by:
Hi, How can I stop hotlinking to a specific file, and I want it to redirect it to a PHP link so I can monitor the number of downloads. Here is my site with the download page:...
1
KevinADC
by: KevinADC | last post by:
Note: You may skip to the end of the article if all you want is the perl code. Introduction Many websites have a form or a link you can use to download a file. You click a form button or click...
7
Curtis Rutland
by: Curtis Rutland | last post by:
Building A Silverlight (2.0) Multi-File Uploader All source code is C#. VB.NET source is coming soon. Note: This project requires Visual Studio 2008 SP1 or Visual Web Developer 2008 SP1 and...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, youll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.