PHP会话变量在表单操作中使用PHP_SELF在标题重定向上丢失

PHP会话变量在表单操作中使用PHP_SELF在标题重定向上丢失

问题描述:

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

I have a multi-step form, let's say for the sake of ease it's 2 steps. First step I want to select a radio button and based on that radio button selection it takes me to a certain page, but I also want that selection stored in a session. I have 2 pages: 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>

Page2.php(A或B)

Page2.php (either A or B)

session_start();
$_SESSION['country'] = $_POST['country'];
<?php echo $_SESSION['country']; ?>

当我有这个条件重定向时,Country选择不会被传递。是否有会话变量和重定向或会话变量和PHP_SELF或某个问题?

The Country choice is not being passed when I have it do this conditional redirect. Is there a problem with session variables and redirects or session variables and PHP_SELF or something?

b
$ b

Page 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>

第2页:

session_start();
<?php echo $_SESSION['country']; ?>

或使用include方法,只需使用一个页面:

Or using include method, just use one page:

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>

,您应该可以使用 echo $ _POST ['country'] 在US_Products.php和CDN_Products.php或

and you should be able to use echo $_POST['country'] on US_Products.php and CDN_Products.php, or