gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 17:10:37 24 4
gpt4 key购买 nike

我有两个文件存储在本地服务器的一个文件夹中,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 文件中完成以及如何完成。

提前致谢!

最佳答案

你可以使用$_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 时,他或她将在注销后被重定向到登录,因为 session 不存在。

希望这对您有所帮助。

关于php - 只允许从另一个 .php 文件访问 .php 文件,而不是直接 url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18237062/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com