在表单操作中使用PHP_SELF进行头重定向时丢失PHP会话变量

在表单操作中使用PHP_SELF进行头重定向时丢失PHP会话变量,php,forms,session,cookies,redirect,Php,Forms,Session,Cookies,Redirect,我有一个多步骤的形式,让我们说,为了方便起见,它是两个步骤。第一步,我想选择一个单选按钮,并根据该单选按钮选择将我带到某个页面,但我也希望该选择存储在会话中。我有两页: page1.php session_start(); if(isset($_POST['post'])) { if (($_POST['country'] == 'US')) { header("Location: US_Products.php"); } elseif (($_POST['country'] == 'CDN'

我有一个多步骤的形式,让我们说,为了方便起见,它是两个步骤。第一步,我想选择一个单选按钮,并根据该单选按钮选择将我带到某个页面,但我也希望该选择存储在会话中。我有两页: page1.php

session_start();

if(isset($_POST['post'])) {
if (($_POST['country'] == 'US')) {
header("Location: US_Products.php"); }
elseif (($_POST['country'] == 'CDN')) {
header("Location: CDN_Products.php"); }
else { die("Error"); }
exit;
}
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<label for="USA">USA:</label>
<input type="radio" name="country" value="US">
<label for="CDN">Canada:</label>
<input type="radio" name="country" value="CDN">
<input type="submit" name="post" value="Go To Filter">
</form>
session_start();
如果(isset($_POST['POST'])){
如果($_POST['country']=='US')){
标题(“Location:US_Products.php”);}
其他($_POST['country']=='CDN')){
标题(“位置:CDN_Products.php”);}
否则{die(“Error”);}
出口
}
当我让它执行此条件重定向时,国家/地区选择未被通过。会话变量和重定向或会话变量和PHP_SELF或其他东西有问题吗?

在重定向之前设置会话变量-帖子丢失,因为重定向本质上是一个常规GET请求

if (($_POST['country'] == 'US')) 
{
  $_SESSION['country'] = $_POST['country'];
  header("Location: US_Products.php"); 
}
elseif (($_POST['country'] == 'CDN')) 
{
  $_SESSION['country'] = $_POST['country'];
  header("Location: CDN_Products.php"); 
}
else 
{ 
  die("Error"); 
}
第1页:

session_start();

if(isset($_POST['post'])) {
    $_SESSION['country'] = $_POST['country'];
    if (($_POST['country'] == 'US')) {
        header("Location: US_Products.php"); }
    elseif (($_POST['country'] == 'CDN')) {
        header("Location: CDN_Products.php"); }
    else { die("Error"); }
    exit;
}
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<label for="USA">USA:</label>
<input type="radio" name="country" value="US">
<label for="CDN">Canada:</label>
<input type="radio" name="country" value="CDN">
<input type="submit" name="post" value="Go To Filter">
</form>
session_start();
如果(isset($_POST['POST'])){
$_SESSION['country']=$_POST['country'];
如果($_POST['country']=='US')){
标题(“Location:US_Products.php”);}
其他($_POST['country']=='CDN')){
标题(“位置:CDN_Products.php”);}
否则{die(“Error”);}
出口
}
或者使用include方法,只需使用一页:

session_start();

if(isset($_POST['post'])) {
    if (($_POST['country'] == 'US')) {
        include("US_Products.php"); }
    elseif (($_POST['country'] == 'CDN')) {
        include("CDN_Products.php"); }
    else { die("Error"); }
    exit;
}
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<label for="USA">USA:</label>
<input type="radio" name="country" value="US">
<label for="CDN">Canada:</label>
<input type="radio" name="country" value="CDN">
<input type="submit" name="post" value="Go To Filter">
</form>
session_start();
如果(isset($_POST['POST'])){
如果($_POST['country']=='US')){
包括(“US_Products.php”);}
其他($_POST['country']=='CDN')){
包括(“CDN_Products.php”);}
否则{die(“Error”);}
出口
}

会话变量可以很好地传输,但POST变量不能。发送位置标题不会发送任何帖子数据。我可以用什么方法来解决这个问题?你太棒了!!!那么在第二页定义它就是把它搞砸了?这可能是一个愚蠢的问题,但是您是否总是定义会话变量来在输入帖子的页面上发布变量?一旦调用
标题
,您就丢失了
$\u GET
$\u POST
数据,因为您转到了另一个页面。因此,您的选项是在调用header之前将其存储在会话变量中,或者通过执行类似于
header(“Location:US\u Products.php?country=$\u POST[country]”的操作通过header调用传递数据
,这将使美国产品的
$\u GET
数组中的
国家/地区
可用
session_start();
<?php echo $_SESSION['country']; ?>
session_start();

if(isset($_POST['post'])) {
    if (($_POST['country'] == 'US')) {
        include("US_Products.php"); }
    elseif (($_POST['country'] == 'CDN')) {
        include("CDN_Products.php"); }
    else { die("Error"); }
    exit;
}
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<label for="USA">USA:</label>
<input type="radio" name="country" value="US">
<label for="CDN">Canada:</label>
<input type="radio" name="country" value="CDN">
<input type="submit" name="post" value="Go To Filter">
</form>