4

如何在不修改 .htaccess 的情况下阻止直接访问某些页面?

有没有办法在每个脚本中使用一些代码来实现这一点?

任何帮助都感激不尽。

4

1 回答 1

8

尝试在第一行添加此代码:

根据 Michael Berkowski 的建议,答案的改进版本如下:

/***************DO NOT ALLOW DIRECT ACCESS************************************/
if ( (strpos( strtolower( $_SERVER[ 'SCRIPT_NAME' ] ), strtolower( basename( __FILE__ ) ) ) ) !== FALSE ) { // NOT FALSE if the script's file name is found in the URL 
  header( 'HTTP/1.0 403 Forbidden' );
  die( '<h2>Direct access to this page is not allowed.</h2>' );
}
/*****************************************************************************/

更新:

/***************DO NOT ALLOW DIRECT ACCESS************************************/
if ( stripos( $_SERVER[ 'REQUEST_URI' ], basename( __FILE__ ) ) !== FALSE ) { // TRUE if the script's file name is found in the URL
  header( 'HTTP/1.0 403 Forbidden' );
  die( "<h2>Forbidden! You don't have permission to access this page.</h2>" );
}
/*****************************************************************************/

此代码可用于保护其他代码使用的不需要通过浏览器访问的具有函数、类等的文件。比如大部分的WP插件、admin和include文件、wp-config.php、functions.php;文件以获取通过 POST 方法(不是 GET)等传输的数据。

于 2012-11-22T00:49:32.750 回答