【问题标题】:allow access to a .php file only from another .php file and not direct url只允许从另一个 .php 文件而不是直接 url 访问 .php 文件
【发布时间】:2013-08-14 16:18:35
【问题描述】:

我在本地服务器的一个文件夹中存储了两个文件,login.php 和 update.php。这两个文件只要输入就可以从任何位置访问:

ip:port/folder/login.php

ip:port/folder/update.php.

我想要做的是阻止用户通过在 url 中输入 update.php 并只允许他们通过以下方式访问 update.php 文件首先转到 login.php(当按下按钮时,login.php 会将它们重定向到 update.php)。

我对 php 和 apache 有点陌生。我不确定这是否应该在 PHP 或 .htaccess 文件中完成以及如何完成。

提前致谢!

【问题讨论】:

  • 好吧,既然您是 php 新手,并且您正在使用没有路由器的单独文件,那么您唯一需要做的就是在 update.php 上检查是否有会话启动,如果是的话,很酷,只要显示 update.php

标签: php apache http


【解决方案1】:

你可以使用$_SESSION

// Let's say this is you update.php
session_start();

if (isset($_SESSION['email']) /* or something like that */)
{                     
    session_unset();
    session_destroy();
    header("Location: login.php");
    exit();
}

// do whateven you need to do and set up $_SESSION variables 
// for example get the user entered info here

// This is how you set session variables
$_SESSION['username'] = ...;
$_SESSION['email']    = ...;

// Then after you did the registration part or something else you wanted to do
// You can redirect the user to any page you want
header("Location: some_other_page.php");

每次用户尝试立即输入update.php,由于会话不存在,他或她将在他们注销后被重定向到登录。

希望这能有所帮助。

【讨论】:

    【解决方案2】:

    不可能有一个用户只能偶尔访问的 URL。

    如果你想要一个登录系统,那就做其他人做的事:

    1. 在登录时设置识别 cookie
    2. 当用户访问受限页面时:
      1. 测试您已识别它们
      2. 测试识别的用户是否有权查看页面
      3. 如果前两个条件之一失败,则将他们重定向到登录页面或给他们一个未经授权的响应

    【讨论】:

      【解决方案3】:

      您可以让它检查推荐网址。您还可以创建用户会话,以便当用户访问更新时。 php,一个变量被验证。会话变量可以在登录时设置为正确的值。 php。

      会话允许您在人们在您的网站上从一个页面到另一个页面时存储变量。

      【讨论】:

        【解决方案4】:

        使用readfile 是一个很好的方法。

        <?php
        // This is the path to your PDF files. This shouldn't be accessable from your
        // webserver - if it is, people can download them without logging in
        $path_to_pdf_files = "/path/to/pdf/files";
        
        session_start();
        
        // Check they are logged in. If they aren't, stop right there.
        if (!isset($_SESSION['logged_in']) || $_SESSION['logged_in'] != true) {
            die("You are not logged in!");
        }
        
        // Get the PDF they have requested. This will only allow files ending in 'pdf'
        // to be downloaded.
        $pdf_file = basename($_GET['file'], ".pdf") . ".pdf";
        
        $pdf_location = "$path_to_pdf_files/$pdf_file";
        
        // Check the file exists. If it doesn't, exit.
        if (!file_exists($pdf_location)) {
            die("The file you requested could not be found.");
        }
        
        // Set headers so the browser believes it's downloading a PDF file
        header("Content-type: application/pdf");
        header("Content-Disposition: inline; filename=$pdf_file");
        $filesize = filesize($pdf_location);
        header("Content-Length: $filesize");
        
        // Read the file and output it to the browser
        readfile($pdf_location);
        
        ?>
        

        这取自wildeeo-ga在谷歌答案上的回答。

        【讨论】:

          猜你喜欢
          • 2014-08-10
          • 1970-01-01
          • 2014-01-25
          • 2016-10-24
          • 1970-01-01
          • 1970-01-01
          • 2014-06-04
          • 2017-06-08
          • 1970-01-01
          相关资源
          最近更新 更多