2012-03-30 31 views
2

我有一個小型網站,其中有幾個PDF可供免費下載。我使用StatCounter來觀察頁面加載的數量。它也顯示我的PDF下載數量,但它只考慮用戶點擊我網站鏈接的下載量。但是,如何通過「外部」訪問PDF(例如直接從Google搜索)?我如何計算這些?有沒有可能使用像StatCounter這樣的工具?在服務器上訪問文件(例如PDF)的次數

謝謝。

+1

如果你使用Apache的重定向* .pdf請求與mod_rewrite到一個PHP腳本。增加計數器,然後讀取實際的PDF內容並將其發送到瀏覽器。 – strkol 2012-03-30 11:01:08

+0

@strkol謝謝!你能舉個例子說明這個重定向命令應該是這樣嗎? – Ivan 2012-03-30 11:14:05

回答

1

的.htaccess(重定向的* .pdf請求的download.php):

RewriteEngine On 
RewriteRule \.pdf$ /download.php 

的download.php:

<?php 
$url = $_SERVER['REQUEST_URI']; 
if (!preg_match('/([a-z0-9_-]+)\.pdf$/', $url, $r) || !file_exists($r[1] . '.pdf')) { 
    header('HTTP/1.0 404 Not Found'); 
    echo "File not found."; 
    exit(0); 
} 

$filename = $r[1] . '.pdf'; 
// [do you statistics here] 
header('Content-type: application/pdf'); 
header("Content-Disposition: attachment; filename=\"$filename\""); 
readfile($filename); 
?> 
0

您將不得不創建一種方法來捕獲來自服務器的請求。

如果您使用的是php,最好的方法是使用mod_rewrite。 如果您使用.net,一個HttpHandler。

您必須處理該請求,調用statcounter,然後將pdf內容發送給用戶。

1

您可以使用來檢查文件被訪問的次數。如果他們提供訪問日誌和日誌分析軟件的訪問權限,請詢問您的託管服務提供商

1
在PHP

,它會是這樣的(未經測試):

$db = mysql_connect(...); 
$file = $_GET['file']; 
$allowed_files = {...}; // or check in database 
if (in_array($file, $allowed_files) && file_exists($file)) { 
    header('Content-Description: File Transfer'); 
    header('Content-Type: application/pdf'); 
    header('Content-Disposition: attachment; filename='.basename($file)); 
    header('Content-Transfer-Encoding: binary'); 
    header('Expires: 0'); 
    header('Cache-Control: must-revalidate'); 
    header('Pragma: public'); 
    header('Content-Length: ' . filesize($file)); 
    ob_clean(); 
    flush(); 
    mysql_query('UPDATE files SET count = count + 1 WHERE file="' . $file . '"') 

    readfile($file); 
    exit; 
} else { 
    /* issue a 404, or redirect to a not-found page */ 
} 
+0

儘管使用PDO或參數化查詢會更好,並且使用mod_rewrite而不是GET變量,因此URL對用戶而言看起來更自然。 – 2012-03-30 11:34:14

相關問題