hfw*_*hfw 1 php pdf wordpress .htaccess apache2
我过去曾在用户未使用以下代码登录时保护 PDF:
RewriteCond %{REQUEST_FILENAME} ^.*(pdf)$
RewriteCond %{HTTP_COOKIE} !^.*wordpress_logged_in.*$ [NC]
RewriteRule . - [R=403,L]
Run Code Online (Sandbox Code Playgroud)
出于某种原因,它停止对我工作。研究表明,也许 wordpress-logged_in 不再相关,因为它是一个黑客漏洞。如果用户未登录,是否有保护 PDF 文档的替代解决方案?
如果您愿意,这些 pdf 不会嵌入到页面中,而是“热链接”。我不是在寻找一个臃肿的插件。只是一个专门保护 PDF 的解决方案。
编辑:
以下是我的完整 htaccess。
# 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>
Run Code Online (Sandbox Code Playgroud)
这是一个比您的解决方案稍重的解决方案,但恕我直言,它仍然比某些“超级保护您的 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]
Run Code Online (Sandbox Code Playgroud)
下载.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);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2248 次 |
| 最近记录: |