473,395 Members | 1,706 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,395 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 3324

"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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.