简体   繁体   English

我想防止人们使用.htaccess直接访问我的php页面

[英]I want to prevent people from accessing my php pages directly using .htaccess

I have a site that is made up of php pages, but they are served to the user through includes based on what I think they need. 我有一个由php页组成的网站,但是根据我认为的需要,它们通过包含给用户的服务。 if they can guess the name of a php file, they can access those pages. 如果他们可以猜出php文件的名称,则可以访问这些页面。 while this is not a security risk at all, i would rather have a way to catch this and redirect them to somewhere else. 虽然这根本不是安全风险,但我宁愿有一种方法来捕获此漏洞并将其重定向到其他地方。

i really want everything to go through the index page unless it is a file that exists (exeption being for any file ending with .php). 我真的希望所有内容都通过索引页面,除非它是一个存在的文件(例如,以.php结尾的任何文件都适用)。

I tried this, didnt work: 我试过了,没用:

RewriteEngine on
RewriteCond %{REQUEST_URI} !(.*\.php$) [NC]
RewriteCond %{REQUEST_FILENAME} !-f [NC]
RewriteRule .* /n/index.php [NC]

也许您可以将php文件放在网络路径之外的目录中?

One way to handle this would be to define a constant in the page doing the including: 一种解决方法是在页面中定义一个常量,包括:

define("access", "legitimate");

Then at the start of each included file: 然后在每个包含文件的开头:

if(!defined('access')){
    header("Location:index.php");
    die(); // make sure to call die() or the rest of the page will be parsed
}

This is the way many frameworks and CMS handle this issue (off the top of my head, Joomla! and CodeIgniter) - you don't need to mess about with .htaccess, and it will also work across various hosting platforms. 这就是许多框架和CMS处理此问题的方式(在我的脑海中,Joomla!和CodeIgniter就是这样)-您无需理会.htaccess,它也可以在各种托管平台上工作。

I think something like this will work for you: 我认为这样的事情将为您工作:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php [L]
RewriteRule \.php$ index.php [L]
RewriteEngine on
RewriteCond %{REQUEST_URI} .*\.php$ [NC]
RewriteRule !^index\.php$ error.php

This works for me, and looks like it should do what you want. 这对我有效,并且看起来应该可以满足您的要求。 Putting the scripts you don't want direct access to outside of the web path is the better answer though - it doesn't require special rules (that may later break and require changing) nor extensions. 不过,将您不想直接访问的脚本放在Web路径之外是更好的答案-它不需要特殊的规则(以后可能会中断并需要更改)或扩展。

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

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