简体   繁体   English

防止直接访问Wordpress插件中的php文件

[英]Prevent direct access to php files in a Wordpress plugin

I am building a Wordpress plugin that should be used on my site only. 我正在构建仅应在我的网站上使用的Wordpress插件。 I have placed it at the regular place under wp-content/plugins/myPlugin. 我将其放置在wp-content / plugins / myPlugin下的常规位置。 In that directory I have placed an index.php to prevent directory listing and also hide the fact that there is a directory with the name myPlugin by sending the request to 404-page of wordpress. 在该目录中,我放置了一个index.php来防止目录列表,并通过将请求发送到404页的wordpress来隐藏存在名为myPlugin的目录的事实。 The code: 编码:

include ('../../../wp-load.php');
header("HTTP/1.0 404 Not Found - Archive Empty");
$wp_query->set_404();
require TEMPLATEPATH . '/404.php';
exit;

This though do not hinder anyone from accessing my php file on myurl.com/wp-content/plugins/myPlugin/myPlugin.php. 但是,这并不妨碍任何人访问myurl.com/wp-content/plugins/myPlugin/myPlugin.php上的我的php文件。 How can I achieve the same 404 for this and other php files in my plugin? 如何在插件中为此和其他PHP文件实现相同的404? It is important to note that these files should be accessible via Wordpress admin though. 重要的是要注意,这些文件应该可以通过Wordpress admin访问。 Maybe wordpress admin sets some global variable when I am accessing a php file via myurl.com/wp-admin/admin.php?page=myPlugin/myPlugin.php ? 也许当我通过myurl.com/wp-admin/admin.php?page=myPlugin/myPlugin.php访问php文件时,WordPress管理员设置了一些全局变量?

Adding to Sammitch's answer, that's what you can use for WordPress: 添加到Sammitch的答案中,这就是您可以用于WordPress的方式:

defined('ABSPATH') or die("Your message here");

ABSPATH is defined in wp-load.php : ABSPATHwp-load.php定义:

/** Define ABSPATH as this file's directory */
define( 'ABSPATH', dirname(__FILE__) . '/' );

Many applications do something like the following in their global header files: 许多应用程序在其全局头文件中执行以下操作:

define('_JEXEC', TRUE); // Joomla, btw

So that you can do something like this in files that you don't want executed outside of the application's context: 这样,您就可以在不想在应用程序上下文之外执行的文件中执行以下操作:

if( !defined('_JEXEC') ) { die('unatuhorized'); }

But, unfortunately, Wordpress does not seem to do something exactly like this, but it does define a metric butt-ton of other constants like WP_USE_THEMES that you can side-jack for your purposes. 但是,不幸的是,Wordpress似乎并没有完全做到这一点,但是它确实定义了诸如WP_USE_THEMES之类的其他常量的度量标准,您可以根据自己的目的进行侧面WP_USE_THEMES

You can also use get_defined_constants() for a listing of what is defined when your plugin runs. 您也可以使用get_defined_constants()列出插件运行时定义的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM