2

我有一个名为的文件startpage.inc.php,我想包含它,但要防止任何人都可以在他的浏览器中打开它。可能吗?

可能的:

<?php
include 'inc/startpage.inc.php';
?>

阻止:

www.example.org/inc/startpage.inc.php
4

2 回答 2

5

你有几个选择。

  • 检查文件中是否定义了常量。如果没有,exit;. 在包含您的startpage.inc.php. 这就是许多免费脚本如何防止不必要的包含。

  • 公开文件夹中没有脚本。将它移到文档根目录下,这样就永远不能直接访问它,只有在通过include或包含时require

于 2015-05-03T20:19:17.917 回答
1

有很多方法可以做到这一点,包括.htaccessand php,即:

使用 PHP:

if(count(get_included_files()) ==1) exit("Direct access denied.");

使用.htaccess文件

<FilesMatch "\.inc\.php$">
    Order deny,allow
    Deny from all
</FilesMatch>

花点时间阅读: best-5-ways-to-prevent-direct-access-to-a-php-file

于 2015-05-03T20:22:38.553 回答