在表单操作中使用PHP_SELF在标头重定向上丢失了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 (either A or B)
session_start();
$_SESSION['country'] = $_POST['country'];
<?php echo $_SESSION['country']; ?>
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?
我有一个多步形式,让我们说为了方便它的两个步骤。 第一步我想选择一个单选按钮,并根据单选按钮选择它将我带到某个页面,但我也希望选择存储在会话中。 我有2页: page1.php p>
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;
}
&lt; form action =“&lt;?php echo $ _SERVER ['PHP_SELF'];?&gt;” method =“post”&gt;
&lt; label for =“USA”&gt; USA:&lt; / label&gt;
&lt; input type =“radio”name =“country”value =“US”&gt;
&lt; label for =“CDN”&gt; Canada:&lt; / label&gt;
&lt; input type =“radio”name =“country”value =“CDN”&gt;
&lt; input type =“submit”name =“post”value =“转到过滤器”&gt;
&lt; / form&gt;
code> pre>
Page2.php(A或B) p>
session_start();
$ _SESSION ['country'] = $ _POST ['country'];
&lt;?php echo $ _SESSION ['country']; ?&gt;
code> pre>
当我执行此条件重定向时,未传递Country选项。 会话变量和重定向或会话变量以及PHP_SELF有什么问题吗? p>
div>
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>
Page 2:
session_start();
<?php echo $_SESSION['country']; ?>
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>
and you should be able to use echo $_POST['country']
on US_Products.php and CDN_Products.php, or
Set the session variable before you redirect - the post is lost, as the redirect is essentially a regular GET request
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");
}