Wordpress保护文件,如果未登录


Wordpress protect file if not logged in

我过去曾使用以下代码保护过用户未登录的PDFS:

RewriteCond %{REQUEST_FILENAME} ^.*(pdf)$
 RewriteCond %{HTTP_COOKIE} !^.*wordpress_logged_in.*$ [NC]
 RewriteRule . - [R=403,L]

出于某种原因,它停止了对我的工作。研究表明,也许wordpress-logged_in不再相关,因为它是一个黑客漏洞。如果用户未登录,是否有替代解决方案来保护PDF文档?

如果您愿意,这些 pdf 不会嵌入在页面上,而是"热链接"。我不是在寻找一个臃肿的插件。只是专门保护PDF的解决方案。

编辑:

以下是我的完整访问。

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /new/
RewriteRule ^index'.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /new/index.php [L]
</IfModule>
# END WordPress

RewriteCond %{REQUEST_FILENAME} ^.*(pdf)$
RewriteRule ^(.*)$ /wp-content/download-protect.php?file=$1 [L]
# disable directory browsing in WordPress
 Options -Indexes
# protect wp-config.php
 <files wp-config.php>
order allow,deny
deny from all
</files>
#  Protect .htaccess
<files ~ "^.*'.([Hh][Tt][Aa])">
order allow,deny
deny from all
satisfy all
</files>

这是一个比你的解决方案稍重的解决方案,但恕我直言,它仍然比一些"超级保护您的 PDF"Wordpress 插件更好

您所要做的就是将download.php文件放在WP安装中的某个位置(例如wp-content文件夹)。然后,您必须将所有对PDF文件的请求重定向到脚本download.php。它包括一些基本的WP内容,因此您可以使用WP功能,例如is_user_logged_in()

.htaccess

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} ^.*(pdf)$
RewriteRule ^(.*)$ /wp-content/download.php?file=$1 [L]

下载.php

require_once('/path/to/wp-config.php');
require_once('/path/to/wp-includes/wp-db.php');
require_once('/path/to/wp-includes/pluggable.php');
if (!is_user_logged_in()) {
    // redirect to login page or show the message + login form
    die; // or exit, wp_redirect etc
}
header("Content-Type: application/octet-stream");
$file = $_GET["file"] .".pdf";
header("Content-Disposition: attachment; filename=" . urlencode($file));   
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
header("Content-Description: File Transfer");            
header("Content-Length: " . filesize($file));
flush(); // this doesn't really matter.
$fp = fopen($file, "r");
while (!feof($fp))
{
    echo fread($fp, 65536);
    flush(); // this is essential for large downloads
} 
fclose($fp); 

我已经实现了与@salaros建议类似的解决方案,但parse_query使用了WordPress操作。

RewriteRule wp-content/uploads('/[A-Za-z0-9_@.'/&+-]+)+'.([A-Za-z0-9_@.'/&+-]+)$ index.php?pre_dir_acc_61co625547=$1&is_direct_access=true&file_type=$2 [QSA,L]

因此,基本上任何具有上述格式(WordPress上的默认文件格式)的PDF文件都将被重定向到我们的插件,我们将在其中检查文件是否受到保护(通过WordPress管理员完成)。

如果是,我们将他们重定向到 404 页面。如果不是,我们将返回原始文件。

我们的插件还生成了一个所谓的"私有URL",供您在其原始URL被阻止后访问该文件。

有关更多信息,请查看我们的防止直接访问插件。

您可以阻止未登录用户(wordpress)仅使用htaccess的PDF下载

这是我阻止pdf,docx,doc,zip的代码。似乎难以阻止的是图像,因为浏览器需要下载主题才能在屏幕上显示它们......

这是我的代码,如果未登录的用户尝试下载文件,则重定向到主页:

<IfModule mod_rewrite.c>
RewriteEngine on
ErrorDocument 403 https://www.yourSITE.com/
RewriteCond %{REQUEST_FILENAME} (.*)
RewriteCond %{HTTP_COOKIE} !wordpress_logged_in_([a-zA-Z0-9_]*) [NC]
RewriteRule '.(zip|doc|docx|pdf)$ – [NC,F,L]
</IfModule>

为了防止并且不会省略 .htaccess 文件中的代码,请在函数.php文件中添加以下代码

add_filter('mod_rewrite_rules','output_htaccess');
function output_htaccess( $rules ){
$nrule = 'RewriteCond %{REQUEST_FILENAME} ^.*(pdf)$
RewriteRule ^(.*)$ /wp-content/themes/theme-name/download.php?file=$1 [L]';
$rules = str_replace('RewriteBase /', $nrule , $rules);
return $rules;
}

在主题文件夹中创建下载.php文件

require_once($_SERVER['DOCUMENT_ROOT']. '/wp-load.php');
if (!is_user_logged_in()) {
        // redirect to login page or show the message + login form
        die('Please login'); // or exit, wp_redirect etc
}
$filename = $_GET["file"];
$filePath = $_SERVER['DOCUMENT_ROOT']. "/".$filename;
header('Content-type:application/pdf');
header('Content-disposition: inline; filename="'.$filename.'"');
header('content-Transfer-Encoding:binary');
header('Accept-Ranges:bytes');
@ readfile($filePath);