简体   繁体   English

仅允许从另一个.php文件访问.php文件,而不允许直接访问url

[英]allow access to a .php file only from another .php file and not direct url

I have two files stored in my local server in a folder, login.php and update.php. 我有两个文件存储在本地服务器的一个文件夹中,分别是login.php和update.php。 These two files are accessible from any location as long as they enter: 只要输入以下两个文件,就可以从任何位置访问它们:

ip:port/folder/login.php

and

ip:port/folder/update.php.

What I am trying to do is prevent the users going to update.php by entering it in the url and only allow them to access the update.php file by first going to login.php ( login.php redirects them to update.php when a button is pressed). 我正在尝试做的是防止用户通过在URL中输入update.php并仅允许他们首先访问login.php来访问update.php文件(当登录时, login.php将他们重定向到update.php) 。按下一个按钮)。

I am kinda new to php and apache. 我是php和apache的新手。 I am not sure if this should be done in PHP or in .htaccess file and how. 我不确定这是否应该在PHP或.htaccess文件中完成以及如何完成。

Thanks in advanced! 提前致谢!

You can use $_SESSION 您可以使用$_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");

Every time user tries to enter the update.php right away, he or she will be redirected to login after they have logged out since the session is not there. 每次用户尝试立即输入update.php时,由于会话不存在,他们注销后将被重定向到登录名。

Hope this helped a little. 希望这能有所帮助。

It is impossible to have a URL that a user can visit only sometimes. URL不可能只有用户有时才能访问。

If you want a login system, then do what everybody else does: 如果您需要登录系统,请执行其他所有人的操作:

  1. Set an identifying cookie on login 设置登录时的识别Cookie
  2. When a user visits a restricted page: 用户访问受限页面时:
    1. Test that you have identified them 测试您是否已识别它们
    2. Test that the identified user is authorised to view the page 测试所标识的用户是否有权查看该页面
    3. Redirect them to the login page or give them a not authorised response if either of the previous two conditions were failed 将它们重定向到登录页面,或者如果前两个条件之一失败,则给他们未经授权的响应

You can have it check the referral url. 您可以检查引荐网址。 You can also create a user session, so when a user accesses update . 您还可以创建用户会话,以便当用户访问update时。 php, a variable is verified. php,已验证变量。 The session variable can be set to a correct value on login. 可以在登录时将会话变量设置为正确的值。 Php. PHP的。

Sessions allow you to store variables as people go from page to page on your website. 通过会话,您可以在人们在您的网站上一页一页地浏览时存储变量。

A great way to do this is with readfile . 一个很好的方法是使用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);

?>

This was taken from wildeeo-ga 's answer on google answers. 这取自wildeeo-ga在Google答案上的答案。

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

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