By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,905 Members | 902 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,905 IT Pros & Developers. It's quick & easy.

PHP file download counter

P: n/a
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
Share this Question
Share on Google+
7 Replies


P: n/a

"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

P: n/a

"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

P: n/a
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

P: n/a

"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

P: n/a
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

P: n/a
"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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.