PHP 会话变量未设置
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10725009/
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 getting set
提问by MB34
In one page of our site, I have this code:
在我们网站的一页中,我有以下代码:
$_SESSION['returnURL'] = "/store/checkout/onepage";
and further down, this button control:
再往下,这个按钮控制:
<button type="button" title="Register Today" class="button" onclick="window.location = '/register/';" id="BecomeMember"><span><span>Become a Member Today</span></span></button>
Now, in the register template, I have this code:
现在,在注册模板中,我有以下代码:
<input type="hidden" name="returnURL" id="returnURL" value="<?php if(isset($_SESSION['returnURL'])) { echo $_SESSION['returnURL']; } else { echo '/'; } ?>" />
But it only shows the value as /.
但它只将值显示为 /。
What could be going on that is causing this?
是什么导致了这种情况?
采纳答案by MB34
What I ended up doing was sending a post variable to the page. The difference in the sessions between ExpressionEngine and Magento makes this prohibitive using session variables as well as cookies.
我最终做的是向页面发送一个 post 变量。ExpressionEngine 和 Magento 之间的会话差异使得这禁止使用会话变量和 cookie。
回答by DaneSoul
first.php
第一个.php
<?php
session_start();
$_SESSION['returnURL'] = "/store/checkout/onepage";
echo '<a href="second.php">Pass session to another page</a>';
?>
second.php
第二个.php
<?php
session_start();
echo 'returnURL = ' . $_SESSION['returnURL'];
?>
So you need to write session_start()in both your files
所以你需要写session_start()在你的两个文件中
回答by George Cummins
To solve this problem, you will need to:
要解决此问题,您需要:
1) Ensure that session_start()is called at the beginning of the script, before anything else.
1) 确保session_start()在脚本的开头,在其他任何事情之前调用。
2) Nothing is unsetting $_SESSIONor $_SESSION['returnURL'].
2)没有什么是令人不安的$_SESSION或$_SESSION['returnURL']。
回答by Steampunk Forge
i was able to get this to work like this
我能够让它像这样工作
session_start();
$returnurl = "/store/checkout/onepage";
$_SESSION['returnURL'] = $returnurl;
回答by Spider
I just fund i had the same kind of issue, sessions working fine in firefox but not chrome, i created a test script that would just create a session and then print out the session_id() in order to see if it was getting created or not, after running this script i noticed that the session_id() would change on every page load and that php was throwing a warning about the date/time not being set. I then added
我只是资助我遇到了同样的问题,会话在 Firefox 中运行良好,但在 chrome 中运行良好,我创建了一个测试脚本,该脚本只会创建一个会话,然后打印出 session_id() 以查看它是否被创建,运行此脚本后,我注意到 session_id() 会在每次页面加载时更改,并且 php 会发出有关未设置日期/时间的警告。然后我添加了
date_default_timezone_set('America/Los_Angeles');
date_default_timezone_set('美国/洛杉矶');
to the start of the script this stoped a new session_id() from getting generated on every page load and fixed the problem. (it might be worth noting that my issue only seemed to show up on my sub domain and not the top level domain)
到脚本开始时,这阻止了在每次页面加载时生成新的 session_id() 并解决了问题。(可能值得注意的是,我的问题似乎只出现在我的子域中,而不是顶级域中)
回答by Hotshot
the server I was working on was full and thus session didn't work as there was no space to store values. Make sure your server has space.
我正在处理的服务器已满,因此会话无法工作,因为没有空间存储值。确保您的服务器有空间。
回答by Rápli András
I've seen many CMSes and frameworks having a different way of handling regular sessions. If the basic two-liner described above does not work (because it interferes with the current software), you can still use cookies for the same functionality. Remember, that cookies do not get deleted on closing the browser, so you need to tell it explicitly when to free up the variable (using unset).
我已经看到许多 CMS 和框架具有不同的处理常规会话的方式。如果上面描述的基本两行不起作用(因为它干扰了当前的软件),您仍然可以使用 cookie 来实现相同的功能。请记住,cookie 不会在关闭浏览器时被删除,因此您需要明确告诉它何时释放变量(使用 unset)。
$_COOKIE["something"] = 'value';
echo $_COOKIE["something"];
unset($_COOKIE["something"]);
回答by Afsal Mt
The session_start() function must be the very first thing in your document. Before any HTML tags.
session_start() 函数必须是文档中的第一件事。在任何 HTML 标签之前。
session_start();

