首页 > 解决方案 > 根据登录用户限制 wordpress 中的 html 文件访问

问题描述

我们在 WordPress 应用程序的文件夹中有 html 文件。我们想限制基于 html 文件的 wordpress 用户登录,我们如何限制 html 文件访问?

htaccess 密码机制不够好,因为用户已经登录 WordPress,再次在弹出窗口中输入凭据是不需要的。

任何指向实现的指针?

标签: htmlwordpressrestrict

解决方案


这是我用于 Wordpress 中受保护文件的解决方案。这些文件位于特定文件夹中,该文件夹中的所有用户都通过 .htaccess 拒绝访问。纯粹通过 wordpress 用户状态进行访问控制。

服务.php:

<?php 
ob_start();
header('Content-Type: html');
header("Expires: 0");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
//this will enable the basic functionality to check for user status
$wpload=$_SERVER['DOCUMENT_ROOT'] . '/wp-load.php';
define( 'WP_USE_THEMES', false ); 
Include_once($wpload);
$loggedin= is_user_logged_in();
//here you can also do more extensive checks. eg: each user has only access to his own folder or files
if(!$loggedin) exit;

$file= sanitize_text_field($_GET['file']);
$file= realpath(dirname(__FILE__) . '/../../../uploads/myplugin/').'/'.$file;

ob_end_clean();
readfile($file);
?>

获取文件的链接将如下所示:

https://mydomain/serve.php?file=my-sample.html

这很好用。一个缺点是,浏览器缓存不适用于此方法,并且在服务器端输出也较慢。html文件应该没有问题。我用它来显示图像,可能增加了 500 毫秒的延迟。

因此,基本上,serve.php 是一个独立文件,它加载基本的 wordpress 文件并检查在查询字符串中传递的参数。


推荐阅读