简体   繁体   English

PHP 会话变量未设置

[英]PHP Session variable not getting set

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?是什么导致了这种情况?

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()

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 $_SESSION or $_SESSION['returnURL'] . 2) 没有什么可以取消$_SESSION$_SESSION['returnURL']

i was able to get this to work like this我能够让它像这样工作

session_start();
$returnurl = "/store/checkout/onepage";
$_SESSION['returnURL'] = $returnurl;        

What I ended up doing was sending a post variable to the page.我最终做的是向页面发送一个 post 变量。 The difference in the sessions between ExpressionEngine and Magento makes this prohibitive using session variables as well as cookies. ExpressionEngine 和 Magento 之间的会话差异使得这禁止使用会话变量和 cookie。

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.我只是资助我遇到了同样的问题,会话在 Firefox 中运行良好,但在 chrome 中运行良好,我创建了一个测试脚本,该脚本只会创建一个会话,然后打印出 session_id() 以查看它是否被创建,运行此脚本后,我注意到 session_id() 会在每次页面加载时更改,并且 php 会发出有关未设置日期/时间的警告。 I then added然后我添加了

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.到脚本开始时,这阻止了在每次页面加载时生成新的 session_id() 并解决了问题。 (it might be worth noting that my issue only seemed to show up on my sub domain and not the top level domain) (可能值得注意的是,我的问题似乎只出现在我的子域中,而不是顶级域中)

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.确保您的服务器有空间。

The session_start() function must be the very first thing in your document. session_start() 函数必须是文档中的第一件事。 Before any HTML tags.在任何 HTML 标签之前。

session_start();
    <!--First Line like as-->
 <?php session_start();?>
 <!-- Now, your php or html codes-->
<html>
  <head>
   .....
   .....

I've seen many CMSes and frameworks having a different way of handling regular sessions.我已经看到许多 CMS 和框架具有不同的处理常规会话的方式。 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.如果上面描述的基本两行不起作用(因为它干扰了当前的软件),您仍然可以使用 cookie 来实现相同的功能。 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).请记住,cookie 不会在关闭浏览器时被删除,因此您需要明确告诉它何时释放变量(使用 unset)。

$_COOKIE["something"] = 'value';
echo $_COOKIE["something"];
unset($_COOKIE["something"]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM