简体   繁体   English

阻止用户直接访问此Eg.rop中的results.php(包含) - >“index.php”(包括) - >“results.php”

[英]Prevent users from directly accessing results.php in this E.g. root (contains)-> “index.php” (includes)-> “results.php”

I want to make it impossible for users to access a certain scripts running on my server. 我想让用户无法访问我服务器上运行的某些脚本。

The best way i can explain this is by explaining a brief description of how the root level of the website works. 我可以解释这个的最好方法是解释网站根级如何工作的简要说明。

ROOT contains 4 scripts: ROOT包含4个脚本:

index.php home.php error.php results.php index.php home.php error.php results.php

In the index file, i have included and directed the user to these 3 files, on certain instances. 在索引文件中,我已经在某些实例中包含并指导用户访问这3个文件。

Now, this causes a bit of a problem, as the index file includes the neccessary controllers and any addittional scripts before the new script is included and the users' point of view changes to the new "webpage". 现在,这会引起一些问题,因为索引文件包含必要的控制器和任何附加脚本,然后才会包含新脚本并且用户的观点会更改为新的“网页”。 I have done this as it provides a very quick experiance for the user, as in the the page load times have become very low. 我这样做了,因为它为用户提供了非常快速的体验,因为页面加载时间变得非常低。

Now these files have been set with the robots no index meta, and are removed from the sitemap. 现在,这些文件已使用机器人无索引元素设置,并从站点地图中删除。 I want to go one step further, and make it impossible for users to access these scripts direcly, as in by typing http://www.mysite.com/results.php 我想更进一步,让用户无法直接访问这些脚本,例如输入http://www.mysite.com/results.php

This is because if they do they are greeted with an ugly, unfuctional page that has not had the layout variables, or main css stylesheet defined. 这是因为如果他们这样做,他们会遇到一个丑陋的,无法解决的页面,它没有布局变量,或者定义了主要的css样式表。

Here is a brief outline of index.php: 以下是index.php的简要概述:

<?php
ob_start();

$activeHome = 'active';

require_once $_SERVER['DOCUMENT_ROOT'] . '/../../includes/config.php';
require_once($docRoot . '/includes/layout.php');

...

include 'home.php';
?>

This script includes any code that configures the 3 other scripts. 此脚本包含配置其他3个脚本的任何代码。 as in 如在

if (isset($_GET['register']))
{
header('Location: http://www.mysite.com/register/');
exit();
}

And here is the same for home/results/error.php 这对于home / results / error.php来说是一样的

<?php

....

echo $head1 . $pageDetails . $head2 . $header .  $pageContent . $footer;
exit;
?>

In these scripts all of the varibles except for pageDetails and pageContent are defined in the layout script, along with the main css file. 在这些脚本中,除了pageDetails和pageContent之外的所有变量都在布局脚本中与主css文件一起定义。

So as you can see by the setup of this website, i do not have very many options left, unless to restrict these pages by a php function which i presume would be fairly complex and more than likely over my head... I assume it would involve heavy use of global or session variables, which is not something i am all to keen about. 因此,你可以通过本网站的设置看到,我没有很多选项,除非通过php函数限制这些页面,我认为这将是相当复杂的,而且可能超过我的头...我认为它将涉及大量使用全局或会话变量,这不是我所有人都热衷的事情。 An easier way, i thought, would be to do this via the htaccess file. 我认为,更简单的方法是通过htaccess文件来完成此操作。 But i am not all that knowlegable when it comes to htaccess commands, and i am not so sure if this is even possible going that route. 但是当涉及到htaccess命令时,我并不是所有人都知道的,而且我不确定这是否有可能走这条路。

Would anyone with a bit more knowledge on something like this, like to offer their opinion or any input or advice? 是否有人对这样的事情有更多的了解,比如提出他们的意见或任何意见或建议?

You have two choices. 你有两个选择。

1. Move the files outside of the web root - This logically makes more sense. 1.将文件移到Web根目录之外 - 这在逻辑上更有意义。 If the files aren't meant to be accessed by the web server they should be outside of the web root. 如果Web服务器不打算访问这些文件,则它们应该位于Web根目录之外。 This would be the preferred method as long as you (and the web server's user and group) have access to the directory that contains your php scripts. 只要您(以及Web服务器的用户和组)可以访问包含您的PHP脚本的目录,这将是首选方法。 It is also more preferable because you don't need Apache specific (or even mod_rewrite specific) .htaccess rules so this will be portable to most other server flavors. 它也更为可取,因为您不需要特定于Apache(甚至特定于mod_rewrite)的.htaccess规则,因此这可以移植到大多数其他服务器风格。 A sample directory structure might looks like this: 示例目录结构可能如下所示:

/
    /www
        /index.php
    /includes
        /home.php
        /error.php
        /results.php

Just make sure that your webserver's user has access to www and includes and your webserver configuration allows you to work outside of your web root (most apache configurations only allow you to work within it). 只需确保您的网络服务器用户可以访问www和includes,并且您的网络服务器配置允许您在Web根目录之外工作(大多数apache配置只允许您在其中工作)。

2. Add htaccess rules to prevent those from being accessed - This is less preferable. 2.添加htaccess规则以防止访问这些规则 - 这不太可取。 If you want to be stubborn or you have a legitimate reason for keeping the files in your web root, this would be the way to do it on Apache. 如果你想要顽固,或者你有合法的理由将文件保存在你的web根目录中,这就是在Apache上做这件事的方法。

RewriteEngine On
RewriteRule ^(home.php|error.php|results.php)$ - [L]

To prevent users from accessing /register/register.php: 要阻止用户访问/register/register.php:

RewriteEngine On
RewriteRule ^register/register.php$ - [L]

如果文件只能从另一个脚本访问,而不是直接通过HTTP访问,则不要将其保留在HTTP服务器的根目录下。

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

相关问题 是否可以通过使result.php文件引用main.css文件中的代码来格式化搜索结果? - Is it possible to format search results by having a results.php file refer to code in a main.css file? WordPress 插件“搜索和过滤” - 在结果中显示自定义帖子类型类别。php 文件 - WordPress Plugin “Search and Filter” - display Custom Post Type Categories in results.php file 如何使用PHP获取服务器的默认页面(例如index.php)? - How can you get the server's default page (e.g. index.php) with PHP? 我如何在jQuery中的页面加载时播放动画,以便它播放index.php而不播放other_page.php - How can I play an animation on page load in jQuery so that it plays for index.php but not e.g. other_page.php 通过直接输入来阻止用户访问php文件 - Prevent users from accessing php files by typing it directly PHP-Laravel 从 /index.php 重定向到 root(/) - PHP-Laravel redirect to root(/) from /index.php 使php方法可从外部root / index.php获得 - Make php method available from outside root/index.php 从上面的目录使用index.php,但包含不同的内容 - Use index.php from directory above but with different includes 如何将PHP重定向到根index.php - how to PHP redirect to the root index.php PHP-不计算index.php中的用户 - PHP - Don't Count Users from index.php
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM