You are here: Re: PHP file download counter « PHP Programming Language « IT news, forums, messages
Re: PHP file download counter

Posted by mistral on 10/25/06 15:42

"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.googlegroups.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/SimpleRedirectLog.php?title=MyFile

M.

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация