问题描述
我在这里想念什么?这是我的PHP文件:
What am I missing here? This is my PHP file:
<?
session_start();
echo print_r($_SESSION);
echo "<br>";
echo print_r($_POST);
// Save input in session
$_SESSION = $_POST;
echo "test1";
?>
<br>
test2
可以从表单提交中访问此文件,并且我确定帖子数据存在.输出为:
This file is accessed from a form submit, and I'm sure the post data exists. The output is:
这对我来说毫无意义. session
内容和post
内容打印没有问题.
and it makes no sense to me. The session
content and post
content are printed without problems.
但是$_SESSION = $_POST;
似乎失败了,脚本的其余部分直到?>
没有给出任何响应,这很明显,因为未打印test1
文本.
But the $_SESSION = $_POST;
appears to fail, and the rest of the script until the ?>
doesn't give any response, which is obvious because the test1
text isn't printed.
我不知道$_SESSION = $_POST;
会发生什么.我在SO和其他地方找到的所有问题都提供了这种在会议中存储帖子数据的方式.
I can't find out what happens with the $_SESSION = $_POST;
. All questions I find on SO and elsewhere gives this way to store post data in the session.
我知道在任何输出之前都需要session_start();
.在这种情况下还需要其他东西吗?
I'm aware that session_start();
is needed prior to any output. Is there something else that is also needed in this case?
我想念什么?
更新-测试文件
完整测试文件(已删除链接)的确切代码是:
The exact code of a full test file (link removed) is:
<?
session_start();
?>
<html>
<head>
</head>
<body>
<?
echo print_r($_SESSION);
echo "<br>";
echo print_r($_POST);
// Save input in session
$_SESSION = $_POST;
echo "test1";
?>
<p>test2</p>
<form method="post" action="test.php">
Type something to test: <input type="text" name="testfield" id="testfield">
<input type="submit" value="Submit">
</form>
</body>
</html>
更新-解决了吗?
好的,现在解决了.但这是STRANGE ...
Okay, it is solved now. But this is STRANGE...
问题显然是由注释引起的. //
阻止脚本在<?..?>
括号内运行.当我删除评论// Save input in session
时,它可以工作...?
The problem appearently is caused by the comment. The //
stops the script from running within the <?..?>
brackets. When I remove the comment // Save input in session
, then it works...?
现在我在这里想念什么?它必须是一些php设置或其他东西.也许某些文件或脚本数据已更改.我猜因为@CodeCaster使它起作用了,所以在我的PC上发生了一些事情……但是注释在其他地方起作用了吗?有人看过吗?
Now WHAT am I missing here? It must be some php setting or something. Or maybe some file or script data that was changed. I guess that since @CodeCaster had it working, something happens on my PC that does it... But comments work anywhere else? Has anyone seen this before?
推荐答案
您是否尝试过使用$_SESSION['name'] = $_POST['name'];
代替?
Have you tried using $_SESSION['name'] = $_POST['name'];
instead?
在我使用PHP编程的整个过程中,我从未使用过$_SESSION = $_POST;
,也从未使用过它.
I've never used $_SESSION = $_POST;
, and neither never seen it, in the whole time I programmed on PHP.
希望这会有所帮助.
这篇关于为什么这个`$ _SESSION = $ _POST`失败?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!