PHP 会话变量不能跨 2 个页面工作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16164735/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP session variable not working across 2 pages
提问by jmill23
Creating form and trying to carry variables accross two pages to a results page. I have tried $GET and $POST and it works fine from page1.php to results.php, but when I change to $SESSION the variable isn't passed or echo'd on results.php. Here's the php code for page1 which won't even work directly to results! Not sure if there is a problem with my code or possibly the server? Page 1.php:
创建表单并尝试将跨两个页面的变量带到结果页面。我已经尝试过 $GET 和 $POST 并且它从 page1.php 到 results.php 都可以正常工作,但是当我更改为 $SESSION 时,该变量不会在 results.php 上传递或回显。这是 page1 的 php 代码,它甚至不能直接用于结果!不确定我的代码或服务器是否有问题?第 1.php 页:
<?php session_start();?>
<?php
$name = $_SESSION['name'];
?>
<FORM action="results.php" method="post" enctype="multipart/form-data" id="questionnaire">
<input type="text" name="name" id="name" />
Results.php:
结果.php:
<?php session_start();?>
<html>
<body>
<?php
$name = $_SESSION['name'];
echo $name; ?>
</body>
</html>
采纳答案by Rubin Porwal
Please try executing following code snippet
请尝试执行以下代码片段
<?php session_start();?>
<?php
if($_SERVER['REQUEST_METHOD']=='POST')
{
$name = $_POST['name'];
$_SESSION['name']=$name;
}
?>
<FORM action="results.php" method="post" enctype="multipart/form-data" id="questionnaire">
<input type="text" name="name" id="name" />
Actually in your code snippet value for $_SESSION['name'] was not set .so I have defined value for session variable with posted value from HTML form
实际上在你的代码片段中 $_SESSION['name'] 的值没有设置。所以我已经定义了会话变量的值,并带有来自 HTML 表单的发布值
回答by Joem Maranan
You forgot the closing tag in you form, and you don't have a submit button
您忘记了表单中的结束标记,并且您没有提交按钮
<FORM action="results.php" method="post" enctype="multipart/form-data" id="questionnaire">
<input type="text" name="name" id="name" />
<input type="Submit" value="Submit">
</FORM>
Then in results.php
然后在results.php
<?php session_start();?>
<html>
<body>
<?php
$_SESSION['name']=$_POST['name'];
echo $_SESSION['name']; ?>
<br><br>
<a href="page2.php">page 2</a>
</body>
</html>
Then i created this page2.php
然后我创建了这个 page2.php
<?php session_start();?>
<html>
<body>
Hi, I am still <?php echo $_SESSION['name'];?>
</body>
</html>
Everything is fine in my side.
在我身边一切都很好。
回答by Indrajeet Singh
Try....
Page 1.php : <?php session_start();
$_SESSION['name'] = isset($_POST['name']) ? $_POST['name'] : '';
?>
<FORM action="results.php" method="post" enctype="multipart/form-data" id="questionnaire">
<input type="text" name="name" id="name" />
Results.php:
<?php session_start();?>
<html>
<body>
<?php
$name = $_SESSION['name'];
echo $name; ?>
</body>
</html>
回答by JOE LEE
Results.php:
结果.php:
<?
session_start();
$name = isset($_POST['name'])?$_POST['name']:'';
if($name){
$_SESSION['name']=$name;
}
?>
<html>
<body>
<?php echo $name; ?>
</body>
</html>
回答by Melki
I think you inverted your variables at this line :
我认为你在这一行反转了你的变量:
<?php
$name = $_SESSION['name'];
?>
You probably wanted to do :
你可能想做:
<?php
$_SESSION['name'] = $name;
?>
回答by soundari
first of all you assign the name variable to session variable
首先,您将名称变量分配给会话变量
<?php session_start();?>
<?php
$_SESSION['name'] = $name;
?>
<FORM action="results.php" method="post" enctype="multipart/form-data" id="questionnaire">
<input type="text" name="name" id="name" />