html - 根据登录用户限制 wordpress 中的 html 文件访问
问题描述
我们在 WordPress 应用程序的文件夹中有 html 文件。我们想限制基于 html 文件的 wordpress 用户登录,我们如何限制 html 文件访问?
htaccess 密码机制不够好,因为用户已经登录 WordPress,再次在弹出窗口中输入凭据是不需要的。
任何指向实现的指针?
解决方案
这是我用于 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 文件并检查在查询字符串中传递的参数。
推荐阅读
- java - 如何修改将 .xls 导出到 xssfworkbook
- c# - 我在加载保存位置上的玩家角色掉在了盒子上
- java - Java,将浮点值列表除以 24 并获取最大值并为其分配变量
- ios - 如何从一个用户向另一个用户发送简单的数据消息并打开一个加载数据的页面?
- authentication - 当 URL 中有 #(主题标签)时,SSO 无法正确重定向
- python - 如何通过标签更改 Treeview 中项目的背景/前景?
- reactjs - 一个简单的 reducer,它共享一个值并分发给我的组件
- swift - 我在 Swift 中遇到 ł、ó、ą 类型字符的问题
- excel - 如何旋转图像并将其移动到特定位置?
- android - 如何从 android.googlesource.com 导入 Android Studio 项目?