拒绝直接访问root中的php文件

时间:2014-03-26 06:28:14

标签: php html include-path

我有一个网站项目,其中每个页面的右侧都是从包含输入字段和按钮的includes文件夹中调用的。一旦用户点击按钮,就会运行php脚本,并根据脚本的结果将用户重定向到thankyou-success.phpthankyou-failure.php文件。这些文件位于根文件夹中。我想阻止用户直接在这些路径中输入网址并直接看到successfailure消息。如何阻止用户直接访问?

目前我正在重定向到文件,如下所示:

//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');
}

除了显示的文本消息外,被调用的两个php文件完全相同。我删除了<head>标记,以保持简单明了。该文件的内容如下:

<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>

2 个答案:

答案 0 :(得分:0)

您可以将这些文件移到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';
}

答案 1 :(得分:0)

一种方法是使用$ _SESSION。提交表格后,您可以:

$_SESSION['result'] = TRUE;

在thankyou-success.php中,您可以这样做:

if ($_SESSION['result']) {
  echo "Success";
  unset($_SESSION['result']);
}
else {
  echo "How did you get here?";
}
相关问题