Php isset $_GET 未按预期工作
Php isset $_GET not working as expected
我正在尝试将 $user_domain
传递给 login.php
。我从 header();
获得 first_page.php
的值。在 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" }
如果 domain
包含 ?
,它会破坏 &key
变量。
您应该将 first_page.php
更改为
if($stmt){
header("Location: returning_user.php?domain=". urlencode($domain)."&key=".$key."");
}
当您在 login.php
中执行 include 'returning_user.php';
时,returning_user.php
中的代码将运行。如果您从顶部跟随该代码路径,您可以看到无论任何参数的内容如何,最终结果都是重定向 - 要么到 login.php
下一个目录(.. 这些路径看起来很奇怪,因为表示您添加的 login.php
与您将用户重定向到的 login.php
或 first_user.php
不同。
我不确定你是否真的想做 include 'returning_user.php';
- 看起来你已经基于该页面上的所有其他内容设置会话,然后将用户重定向到目的地,而不是让文件被包含(当它像这样被包含时,您正在将变量导入当前范围 - 不需要像那样的会话)。
我看到你的沮丧。首先,您需要从登录中删除 returning_user.php。
然后,在登录时用这个替换你的代码,用这个:
session_start();
if(isset($_SESSION['returning_user'] == "TRUE")){
echo "Success!";
}
此外(这通常是一个不受欢迎的观点),为了避免使用 headers,这可能会在其他地方引起其他问题,我更喜欢结束我的 PHP 标签,并放置以下 Javascript
<script>location.replace("whatever.php");</script>
然后立即拿起我的 PHP 标签。
之前:
你的代码有问题,我会尽力解释:
- 您,用户,在
first_page.php
。
- 从那里你被重定向到
returning_user.php
。 GET 值("domain" 和 "key")也被传递。
returning_user.php
读取 GET 值(现在已设置),将它们写入会话并将您重定向到 login.php
(也没有传递 GET 值)。 注意:从技术上讲,您将两个 GET 值传递给页面 (returning_user.php
),以便在会话中将它们保存在那里。我认为不需要此步骤;您可以直接将它们保存在 first_page.php
. 中的会话中
- 在
login.php
中包含页面 returning_user.php
。它尝试再次读取 GET 值。但是,因为 GET 值不是任何设置,您将被重定向到 first_page.php
.
另一个问题是@MatsLindh 描述的问题。
之后:
所以我对你想要实现的目标提出了一些想法,并提出了以下解决方案:
- 您,用户,在
first_page.php
。
- 您填写表格并提交。
- 提交后,值 "domain"、"key" 和 "returning_user" 被写入会话。
- 然后您将被重定向到
login.php
。
- 在
login.php
中包含一个名为 protector.php
的文件。 protector.php
检查会话值 "domain" 和 "key" 是否已设置。如果不是,则会重定向到 first_page.php
。
- 如果
protector.php
中的会话变量验证成功,则可以处理login.php
中的进一步代码。喜欢:阅读会话变量 "returning_user" 和 在那里做一些惊人的事情 :-)
备注:
- 您可以将
protector.php
视为 returning_user.php
的替代品。
- 您可以在所有需要 "protected" 的页面中包含
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...';
}
我正在尝试将 $user_domain
传递给 login.php
。我从 header();
获得 first_page.php
的值。在 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" }
如果 domain
包含 ?
,它会破坏 &key
变量。
您应该将 first_page.php
更改为
if($stmt){
header("Location: returning_user.php?domain=". urlencode($domain)."&key=".$key."");
}
当您在 login.php
中执行 include 'returning_user.php';
时,returning_user.php
中的代码将运行。如果您从顶部跟随该代码路径,您可以看到无论任何参数的内容如何,最终结果都是重定向 - 要么到 login.php
下一个目录(.. 这些路径看起来很奇怪,因为表示您添加的 login.php
与您将用户重定向到的 login.php
或 first_user.php
不同。
我不确定你是否真的想做 include 'returning_user.php';
- 看起来你已经基于该页面上的所有其他内容设置会话,然后将用户重定向到目的地,而不是让文件被包含(当它像这样被包含时,您正在将变量导入当前范围 - 不需要像那样的会话)。
我看到你的沮丧。首先,您需要从登录中删除 returning_user.php。
然后,在登录时用这个替换你的代码,用这个:
session_start();
if(isset($_SESSION['returning_user'] == "TRUE")){
echo "Success!";
}
此外(这通常是一个不受欢迎的观点),为了避免使用 headers,这可能会在其他地方引起其他问题,我更喜欢结束我的 PHP 标签,并放置以下 Javascript
<script>location.replace("whatever.php");</script>
然后立即拿起我的 PHP 标签。
之前:
你的代码有问题,我会尽力解释:
- 您,用户,在
first_page.php
。 - 从那里你被重定向到
returning_user.php
。 GET 值("domain" 和 "key")也被传递。 returning_user.php
读取 GET 值(现在已设置),将它们写入会话并将您重定向到login.php
(也没有传递 GET 值)。 注意:从技术上讲,您将两个 GET 值传递给页面 (returning_user.php
),以便在会话中将它们保存在那里。我认为不需要此步骤;您可以直接将它们保存在first_page.php
. 中的会话中
- 在
login.php
中包含页面returning_user.php
。它尝试再次读取 GET 值。但是,因为 GET 值不是任何设置,您将被重定向到first_page.php
.
另一个问题是@MatsLindh 描述的问题。
之后:
所以我对你想要实现的目标提出了一些想法,并提出了以下解决方案:
- 您,用户,在
first_page.php
。 - 您填写表格并提交。
- 提交后,值 "domain"、"key" 和 "returning_user" 被写入会话。
- 然后您将被重定向到
login.php
。 - 在
login.php
中包含一个名为protector.php
的文件。protector.php
检查会话值 "domain" 和 "key" 是否已设置。如果不是,则会重定向到first_page.php
。 - 如果
protector.php
中的会话变量验证成功,则可以处理login.php
中的进一步代码。喜欢:阅读会话变量 "returning_user" 和 在那里做一些惊人的事情 :-)
备注:
- 您可以将
protector.php
视为returning_user.php
的替代品。 - 您可以在所有需要 "protected" 的页面中包含
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...';
}