拒绝直接访问根目录中的php文件

拒绝直接访问根目录中的php文件,php,html,include-path,Php,Html,Include Path,我有一个网站项目,其中每个页面的右侧都是从includes文件夹调用的,该文件夹包含一个输入字段和一个按钮。用户单击按钮后,将运行php脚本,并根据脚本的结果将用户重定向到thankyou success.php或thankyou failure.php文件。这些文件位于根文件夹中。我希望防止用户直接键入指向这些路径的url并直接看到成功或失败消息。如何防止用户直接访问 目前,我正在重定向到以下文件: //if found this email in our database if($count

我有一个网站项目,其中每个页面的右侧都是从
includes
文件夹调用的,该文件夹包含一个输入字段和一个按钮。用户单击按钮后,将运行php脚本,并根据脚本的结果将用户重定向到
thankyou success.php
thankyou failure.php
文件。这些文件位于根文件夹中。我希望防止用户直接键入指向这些路径的url并直接看到
成功
失败
消息。如何防止用户直接访问

目前,我正在重定向到以下文件:

//if found this email in our database
if($count==1)
{
header('Location: thankyou-success.php');
}
else 
{
//echo "Cannot send Confirmation link to your e-mail address";
header('Location: thankyou-failure.php');
}
<body>
    <!-- header start here -->

   <?php include("includes/header.php") ?>             

    <!-- header end here -->

    <!-- page title start here -->
    <section id="pagetitle-container">
        <div class="row">
            <div class="twelve columns">
                <div id="pagetitle-border">
                <div id="breadcrumb">
                        <ul>
                        <i class="fa fa-caret-right"></i> &nbsp;
                            <li><a href="index.php">Home</a></li>


                        </ul>
                    </div>
                 <p>&nbsp;<br></p>

                 <div class="twelve columns">
                <p>Unable to send the activation email to the email address provided. Please confirm and try again.</p>

                </div>
            </div>
        </div>
    </section>
    <!-- page title end here -->

    <!-- content section start here -->
    <section id="content-wrapper">      
        <div class="row">




            </div>            
        </div>      
    </section>
    <!-- content section end here -->

    <footer>    

    <?php include("includes/footer.php") ?>

          </footer>  

<script>$('#noscript').remove();</script>





<!-- pageloader part 2 start -->




<!-- pageloader part 2 ends -->


</body>
</html>
所调用的两个php文件完全相同,只是显示了文本消息。我已经删除了
标签,以使事情简单明了。文件内容如下:

//if found this email in our database
if($count==1)
{
header('Location: thankyou-success.php');
}
else 
{
//echo "Cannot send Confirmation link to your e-mail address";
header('Location: thankyou-failure.php');
}
<body>
    <!-- header start here -->

   <?php include("includes/header.php") ?>             

    <!-- header end here -->

    <!-- page title start here -->
    <section id="pagetitle-container">
        <div class="row">
            <div class="twelve columns">
                <div id="pagetitle-border">
                <div id="breadcrumb">
                        <ul>
                        <i class="fa fa-caret-right"></i> &nbsp;
                            <li><a href="index.php">Home</a></li>


                        </ul>
                    </div>
                 <p>&nbsp;<br></p>

                 <div class="twelve columns">
                <p>Unable to send the activation email to the email address provided. Please confirm and try again.</p>

                </div>
            </div>
        </div>
    </section>
    <!-- page title end here -->

    <!-- content section start here -->
    <section id="content-wrapper">      
        <div class="row">




            </div>            
        </div>      
    </section>
    <!-- content section end here -->

    <footer>    

    <?php include("includes/footer.php") ?>

          </footer>  

<script>$('#noscript').remove();</script>





<!-- pageloader part 2 start -->




<!-- pageloader part 2 ends -->


</body>
</html>


无法将激活电子邮件发送到提供的电子邮件地址。请确认并重试

$(“#noscript”).remove();
您可以将这些文件移到web根目录之外,并从单击按钮时运行的php脚本中包含这些文件

docroot是在web服务器配置中定义的。假设您的docroot是
/var/www/website/public
,则需要将不希望直接访问的文件移动到此文件夹之外的某个位置,例如:
/var/www/website/files/
。然后,您需要在主脚本中包含这些文件,而不是重定向用户:

main.php:

if ($success) {
  include(dirname(__FILE__) . '/../files/thankyou-success.php';
} else {
  include(dirname(__FILE__) . '/../files/thankyou-failure.php';
}

一种方法是使用$\u会话。提交表格后,您可以:

$_SESSION['result'] = TRUE;
if ($_SESSION['result']) {
  echo "Success";
  unset($_SESSION['result']);
}
else {
  echo "How did you get here?";
}
在Thankyu-success.php中,您可以执行以下操作:

$_SESSION['result'] = TRUE;
if ($_SESSION['result']) {
  echo "Success";
  unset($_SESSION['result']);
}
else {
  echo "How did you get here?";
}

使用会话。如果会话设置为允许用户访问页面。其他明智的做法是不。^与减少会话的使用开销相比,只需在主脚本中设置一个变量,并在任何包含的文件顶部检查它,正如这里的第一个回答:您能简要介绍一下
谢谢失败.php
谢谢成功.php
中的实际内容吗?这样,我可以建议一种简单的拒绝直接访问的方法。@Tzar有关这些详细信息,请参阅上述问题中的
Edit
。您能进一步解释一下吗?这也行不通!用户正在重定向到那些php文件!意味着他的php文件包含完整的代码。那么它将如何工作呢?如果当前页面上没有打印任何内容,这应该不会是一个问题。这可以根据设计来完成,但这行不通。。一旦设置了会话,用户仍然可以直接访问文件!