9

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?

7
  • 1
    session_start() is called elsewhere. Commented May 23, 2012 at 17:33
  • I printed out the $_SESSION array in the register template and it was an empty array. Commented May 23, 2012 at 17:35
  • Is te register template being included with the above page an or is the variable being properly passes? What is your code for the template Commented May 23, 2012 at 17:39
  • Various possibilities: The session is getting closed. The variable is being unset. The template is applied before session_start() is called. We would need to see more code to determine the real source of the problem. Commented May 23, 2012 at 17:39
  • The page where I'm setting the variable is a Magento onepage checkout screen. The register template is an ExpressionEngine template. Could it be that because of the disparity of these two frameworks that they have their own sessions? How can I setup one that will be used for both>? Commented May 23, 2012 at 17:48

9 Answers 9

13

first.php

<?php
session_start();
$_SESSION['returnURL'] = "/store/checkout/onepage";
echo '<a href="second.php">Pass session to another page</a>';
?>

second.php

<?php
session_start();
echo 'returnURL = ' . $_SESSION['returnURL'];
?>

So you need to write session_start() in both your files

Sign up to request clarification or add additional context in comments.

4 Comments

If I place session_start() in this file, I get Session has already been started warning.
You need to describe the structure of your scripts more detailed: which file session starts, how you move to another file and so on. The code I writed is just general approach.
See comments in original post.
Having session_start(); at the start of BOTH of my files was my issue. I had session_start(); at the beginning of my login page, but not the next page! Thanks!
3

To solve this problem, you will need to:

1) Ensure that session_start() is called at the beginning of the script, before anything else.

2) Nothing is unsetting $_SESSION or $_SESSION['returnURL'].

3 Comments

If I place session_start() in this file, I get Session has already been started warning.
Output buffering will fix that issue
No can do, see comments in original post.
2

i was able to get this to work like this

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

1 Comment

That is exactly what DaneSoul did.
1

The session_start() function must be the very first thing in your document. Before any HTML tags.

session_start();

Comments

0

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.

Comments

0

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

date_default_timezone_set('America/Los_Angeles');

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)

Comments

0

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.

Comments

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

2 Comments

Please explain your answer.
Is this an answer? This was already suggested a few years ago.
-1

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.