【问题标题】:Php isset $_GET not working as expectedPHP isset $_GET 没有按预期工作
【发布时间】:2017-10-22 21:53:47
【问题描述】:

我正在尝试将$user_domain 传递给login.php。我从first_page.phpheader(); 获得值。在returning_user.php 中,我设置了一个变量$_SESSION['returning_user'],让我的程序知道在login.php 中要做什么。如果一切顺利,用户将留在login.php。如果出现问题,用户将被重定向到first_page.php,并且必须重新提交表单。我现在面临的问题是当我在login.php 中包含returning_user.php 时,用户被返回到first_page.php。当我删除它时,用户停留在login.php 但似乎if() 语句之间的代码没有被执行。我不知道我做错了什么。任何帮助将不胜感激。

first_page.php

//rest of code above
if($stmt){
header("Location: returning_user.php?domain=".$domain."&key=".$key."");
exit;
}

returning_user.php

session_start();
if(isset($_GET['key'])  && isset($_GET['domain']) ){

//get domain variable
$user_domain = $_GET['domain'];

//put key variable in session
$_SESSION['user_domain'] = $user_domain;

//create a returning_user session
$_SESSION['returning_user']="TRUE";

//direct user to login page
header("location:../login.php");
exit;

}else{
 header("location : first_page.php");
 exit;
   }

login.php

 session_start();
 include 'returning_user.php';

    if(isset($_SESSION['returning_user']) && $_SESSION['returning_user']=="TRUE"){
    //do something amazing here
}

var_dump($_GET)

array(2) { ["domain"]=> string(10) "mydomain" ["key"]=> string(7) "7024158" } 

【问题讨论】:

  • 那么var_dump($_GET) 告诉你什么?
  • 我的观点是,$domain$key 的值是问题所在。所以,正如@MatsLindh 亲切地问的那样,您应该向我们提供$_GET 数组的输出。如果您忽略我们的 cmets,那么您希望我们如何帮助您?
  • 对不起,但我不买公认的答案。两个 GET 数组/变量的值是多少?我看到 $domain$user_domain 对我来说,这告诉我一些没有正确定义的东西。我已经用示例值测试了你的代码,它没有失败。
  • @Fred-ii-: 测试代码后,你是对的,问题不在return.php,而是在login.php
  • include(returning_user.php); 如果这是实际的语法,它会出错并且很可能会抛出未定义的常量通知。它需要用引号include('returning_user.php'); 括起来,如果不是,则应该在所有使用会话的页面内启动会话。使用错误报告php.net/manual/en/function.error-reporting.php 查看它是否确实返回错误。 exit;也应该在header之后使用,否则你的代码可能要继续执行。

标签: php session-variables getvalue


【解决方案1】:

之前:

您的代码有问题,我会尽力解释:

  • 您,用户,在first_page.php 上。
  • 从那里你被重定向到returning_user.php。 GET 值(“domain”和“key”)也被传递。
  • returning_user.php 读取 GET 值(现在是 SET),将它们写入会话并将您重定向到 login.php(也不传递 GET 值)。 注意:从技术上讲,您将两个 GET 值传递给页面 (returning_user.php) 以便在会话中保存它们。这一步在我看来是不需要的;您可以直接将它们保存在会话中 first_page.php
  • login.php 中包含returning_user.php 页面。它尝试再次读取 GET 值。但是,由于 GET 值不是 ANYMORE SET,您将被重定向到 first_page.php

另一个问题是@MatsLindh 描述的问题。

之后:

所以我对您想要实现的目标提出了一些想法,并提出了以下解决方案:

  • 您,用户,在first_page.php 上。
  • 您填写表格并提交。
  • 提交后,“domain”、“key”和“returning_user”值将写入会话中。
  • 然后你被重定向到login.php
  • login.php 中包含一个名为protector.php 的文件。 protector.php 检查是否设置了会话值“域”和“键”。如果没有,则会重定向到 first_page.php
  • 如果protector.php 中的会话变量验证成功,则可以处理login.php 中的进一步代码。比如:阅读会话变量“returning_user”并在那里做一些惊人的事情 :-)

注意事项:

  • 您可以将 protector.php 替换为您的 returning_user.php
  • 您可以在所有需要“保护”的页面中包含protector.php
  • 我会将first_page.php 重命名为dashboard.php
  • first_page.php 中,我实现了一个表单,以便能够在从另一个页面重定向到first_page.php 的情况下在屏幕上显示某些内容,并在单击按钮时开始重定向到login.php,例如提交表单时。

祝你好运!

first_page.php

<?php
session_start();

// Operations upon form submit.
if (isset($_POST['submitButton'])) {
    $stmt = TRUE;
    $domain = 'mydomain';
    $key = '7024158';

    if ($stmt) {
        $_SESSION['domain'] = $domain;
        $_SESSION['key'] = $key;
        $_SESSION['returning_user'] = 1; // Don't use 'TRUE'.

        header('Location: login.php');
        exit;
    }
}
?>
<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8" />

        <title>Demo</title>

        <style type="text/css">
            .header, .section { padding: 10px; }
            .header { border-bottom: 1px solid #ccc; background-color: #eee; }
            .section div { padding-bottom: 10px; }
            .section button { padding: 10px; color: #fff; border: none; width: 200px; background-color: #269abc; }
        </style>
    </head>
    <body>

        <header class="header">
            <h3>
                Welcome! You are on the first page ;-)
            </h3>
        </header>

        <section class="section">
            <form action="" method="post">
                <div>
                    Here comes some form content...
                </div>
                <button type="submit" name="submitButton">
                    Submit
                </button>
            </form>
        </section>

    </body>
</html>

protector.php

<?php

session_start();

// Check domain and key.
if (!isset($_SESSION['domain']) || !isset($_SESSION['key'])) {
    header('Location: first_page.php');
    exit;
}

login.php

<?php

require_once 'protector.php';

echo 'Page validation successful.';
echo '<br/><br/>';

if (isset($_SESSION['returning_user']) && $_SESSION['returning_user']) {
    echo 'I will do something amazing here...';
}

【讨论】:

    【解决方案2】:

    如果domain 包含?,它会破坏&amp;key 变量。

    您应该将您的 first_page.php 更改为

    if($stmt){
        header("Location: returning_user.php?domain=". urlencode($domain)."&key=".$key."");
    }
    

    【讨论】:

    • 我的错。应该是urlencode。没有下划线。查看我的编辑,
    • $key 怎么样?它会产生同样的麻烦。
    【解决方案3】:

    当您在login.php 中执行include 'returning_user.php'; 时,returning_user.php 中的代码会运行。如果您从顶部遵循该代码路径,您可以看到无论任何参数的内容如何,​​最终结果都是重定向 - 要么到 login.php 一个目录下(.. 这些路径看起来很奇怪,因为这意味着您添加的login.php 与您将用户重定向到的login.phpfirst_user.php 不同。

    我不确定您是否真的想要做include 'returning_user.php'; - 似乎您已经基于该页面上的所有其他内容设置会话,然后将用户重定向到目标,而不是包含文件(和当它被这样包含时,您正在将变量导入当前范围 - 不需要像这样的会话)。

    【讨论】:

    • 当我从login.php 中删除returning_user.php 时它可以工作。但就像您已经知道的那样,我正在尝试使用returning_user.php,就像我们使用session.php 来保护页面不被浏览器访问一样。如果我删除 returning_user.php,工作流可以工作,但 login.php 不再受到保护。也许我不明白如何正确地做到这一点。
    【解决方案4】:

    我明白你的沮丧。首先,您需要从 Login 中删除 returned_user.php。

    然后,在登录时将您的代码替换为:

    session_start();
    
    if(isset($_SESSION['returning_user'] == "TRUE")){
    echo "Success!";
    }
    

    另外(这通常是一个不受欢迎的观点),为了避免使用标题,这可能会导致其他地方的其他问题,我更喜欢结束我的 PHP 标记,并放置以下 Javascript

     <script>location.replace("whatever.php");</script>
    

    然后立即拿起我的 PHP 标记。

    【讨论】:

    • 但我不希望人们能够从浏览器访问login.php。这是一个受保护的页面。
    猜你喜欢
    • 2014-03-12
    • 2012-01-16
    • 2011-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多