2016-11-12 197 views
0

我想拒絕訪問文件! 我發送PDF鏈接給這樣一個朋友:http://www.domain.com/pdf/name.pdf 當我的朋友嘗試訪問該pdf鏈接我想拒絕訪問和重定向到登錄頁面,在這裏我插入用戶名和密碼來訪問該pdf!拒絕訪問特定文件,但允許在登錄後訪問該文件

謝謝你的一切!

更新:之後我搜索和閱讀一些東西,我犯了這樣的,是工作的漂亮,上面是代碼:

RewriteEngine on 
 
RewriteCond %{HTTP_REFERER} !^http://(www\.)?yourdomainname/pdf/ [NC] 
 
RewriteCond %{HTTP_REFERER} !^http://(www\.)?yourdomainname/pdf/.*$ [NC] 
 
RewriteRule ^pdf(/.*)\.(gif|jpg|pdf)$ - [F] 
 
ErrorDocument 403 /error/index.php

+0

我建議你開始閱讀一些基本單證。這是標準的,有據可查的,在互聯網上很容易找到。 – arkascha

+0

設置一個CMS,它提供對資源的私有訪問。然後,您可以爲您的朋友添加一個帳戶。 –

回答

1

你應該使用簡單的.htaccess驗證

/path/to/pdf/.htaccess

AuthType Basic 
AuthName "protected area" 
AuthUserFile /path/to/pdf/.htpasswd 
Require valid-user 

/path/to/pdf/.htpasswd

口令必須被加密後和base64編碼。

myusername:$apr1$EzX1xHNv$NgbgAnflbfzjI0Vhxwv8q. 

你可以爲了得到正確的密碼值,爲您的htpasswd文件運行下面的PHP腳本

<?php 
// Password to be encrypted for a .htpasswd file 
$clearTextPassword = 'some password'; 

// Encrypt password 
$password = crypt($clearTextPassword, base64_encode($clearTextPassword)); 

// Print encrypted password 
echo $password; 
?> 

時,有人會嘗試連接到這個網址,他們將被提示進行身份驗證

enter image description here

+0

您也可以使用程序[htpasswd](https://httpd.apache.org/docs/current/programs/htpasswd.html) –

0

你需要把一些邏輯在服務器端之前donwload的PDF文件,首先你需要一臺服務器頁面是c如果用戶登錄(檢查會話在PHP手冊),只有當用戶有會話時執行登錄才能下載PDF文件(檢查從PHP下載pdf文件),如果用戶沒有登錄你重定向用戶到登錄頁面,最後在登錄頁面服務器端,當你檢查用戶有效時,你可以設置用戶在數據庫中的數據,例如。

一個非常簡單的例子是這樣的:

//donwloadpdf.php 
if (session_status() == PHP_SESSION_NONE) { 

    session_start(); 
} 

if (!isset($_SESSION['user_id'])) { //user not logged in 

    redirect('login_page.php'); 

} 

//user logged in 
header("Content-type:application/pdf"); 

// It will be called downloaded.pdf 
header("Content-Disposition:attachment;filename='downloaded.pdf'"); 

// The PDF source is in original.pdf 
readfile("PD_FILE.pdf"); 

你需要閱讀有關會議和下載文件,也需要把登錄的對象有自己的責任。

而在服務器端的login_page.php的一些很簡單的例子是這樣的:

//login_page.php 
//if is POST 
//put some real logic to check if the user is valid 
if (true) { 

    $_SESSION['user_id'] = 1; //put real data from database 

} 
//end of logic to check if the user is valid 
相關問題