假设您在 xampp 中(最好的 windows 原因确保 cmod 写入权限已打开)文件夹 0 并且您可以访问 http://localhost/0/ 因为服务器已打开,
假设您使用这样的链接来访问每个 pdf 文件:
http://localhost/0/file1.pdf
http://localhost/0/file2.pdf
假设您的 php.ini 设置允许运行 error_log 函数
你有这些文件
.htaccess
RewriteEngine On
RewriteBase /0/
RewriteRule ^.*\.pdf$ ?countfile=$0 [R=301,L,QSA]
# i'm not so pro with .htaccess so i used work based on
#https://stackoverflow.com/questions/10949685/htaccess-redirect-file-type
#https://stackoverflow.com/questions/1231067/htaccess-rewrite-for-query-string
#https://stackoverflow.com/questions/20439192/rewrite-url-relative-to-current-directory
index.php
<?php
isset($_REQUEST['countfile'])and error_log($_REQUEST['countfile']."\r\n",3,'logpdf.txt');
header('');
//include($_REQUEST['countfile']);
header("Content-type:application/pdf");
header("Content-Disposition:inline;filename='".$_REQUEST['countfile']);
readfile($_REQUEST['countfile']);
?>
results.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="Generator" content="CustaRoolez">
<title>just for fun</title>
</head>
<body>
<?php
if(file_exists('logpdf.txt')){
$files=file('logpdf.txt');
$statistics=array_count_values($files);
foreach($statistics as $file => $cnt){
echo 'the file: '.$file.' was accesed by'.$cnt.' times<br>';
}
}
?>
</body>
</html>
那么应该是记录访问统计以计算 pdf 访问文件的最快的自定义方法
在https://www.real-domain-whaterver.extension
.htaccess 可能是这样的(你应该设置路由,不是自动的)
所以https://www.real-domain-whaterver.extension/ <---- '/':
RewriteEngine On
RewriteBase /
RewriteRule ^.*\.pdf$ ?countfile=$0 [R=301,L,QSA]
请记住,您应该有权在您可以修改的真实公共领域上进行写作
在 index.php
error_log($_REQUEST['countfile']."\r\n",3,'logpdf.txt');
到
error_log($_REQUEST['countfile']."\r\n",3,'/someFOLDERtoLOG/logpdf.txt');
和results.php
if(file_exists('logpdf.txt')){
$files=file('logpdf.txt');
到
if(file_exists('/someFOLDERtoLOG/logpdf.txt')){
$files=file('/someFOLDERtoLOG/logpdf.txt');
并确保创建 someFOLDERtoLOG 并设置 cmod 775 或验证是否已经(取决于管理员设置)