Bluehost.com Web Hosting $6.95

PHP file download counter

This is a discussion on PHP file download counter within the PHP Language forums, part of the PHP Programming Forums category; I use htaccess to protect directory and granting access to download file only for the authorized users. Just want implement ...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 10-23-2006
mistral
 
Posts: n/a
Default 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.

Reply With Quote
  #2 (permalink)  
Old 10-24-2006
Johnny
 
Posts: n/a
Default Re: PHP file download counter


"mistral" <polychrom@softhome.net> wrote in message
news:1161631139.229252.75610@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...


Reply With Quote
  #3 (permalink)  
Old 10-24-2006
mistral
 
Posts: n/a
Default Re: PHP file download counter


"Johnny писал(а):
"
> "mistral" <polychrom@softhome.net> wrote in message
> news:1161631139.229252.75610@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.

Reply With Quote
  #4 (permalink)  
Old 10-24-2006
petersprc@gmail.com
 
Posts: n/a
Default Re: PHP file download counter

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" <polychrom@softhome.net> wrote in message
> > news:1161631139.229252.75610@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.


Reply With Quote
  #5 (permalink)  
Old 10-24-2006
mistral
 
Posts: n/a
Default Re: PHP file download counter


"petersprc@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" <polychrom@softhome.net> wrote in message
> > > news:1161631139.229252.75610@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.

Reply With Quote
  #6 (permalink)  
Old 10-24-2006
petersprc@gmail.com
 
Posts: n/a
Default Re: PHP file download counter

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:
> "petersprc@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" <polychrom@softhome.net> wrote in message
> > > > news:1161631139.229252.75610@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.


Reply With Quote
  #7 (permalink)  
Old 10-25-2006
mistral
 
Posts: n/a
Default Re: PHP file download counter

"petersprc@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:

"petersprc@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" <polychrom@softhome.net> wrote in message
> news:1161631139.229252.75610@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.

Reply With Quote
  #8 (permalink)  
Old 10-26-2006
petersprc@gmail.com
 
Posts: n/a
Default Re: PHP file download counter

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.


Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 12:25 PM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0