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?

MB34
  • 4,210
  • 12
  • 59
  • 110
  • 1
    session_start() is called elsewhere. – MB34 May 23 '12 at 17:33
  • I printed out the $_SESSION array in the register template and it was an empty array. – MB34 May 23 '12 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 – Ray May 23 '12 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. – George Cummins May 23 '12 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>? – MB34 May 23 '12 at 17:48
  • WOW, I even modified it to use a $_COOKIE variable and it still didn't work. There seems to be some weird differences here that I'm not aware of. – MB34 May 23 '12 at 18:06
  • Looks like cookie domains are setup differently between the 2 systems. THat can cause this behavior so I'll have to work on this a little more. – MB34 May 23 '12 at 20:18

9 Answers9

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

DaneSoul
  • 4,491
  • 3
  • 21
  • 37
  • If I place session_start() in this file, I get Session has already been started warning. – MB34 May 23 '12 at 17:46
  • 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. – DaneSoul May 23 '12 at 17:48
  • See comments in original post. – MB34 May 23 '12 at 17:51
  • 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! – Joe Jun 29 '16 at 11:42
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'].

George Cummins
  • 28,485
  • 8
  • 71
  • 90
2

i was able to get this to work like this

session_start();
$returnurl = "/store/checkout/onepage";
$_SESSION['returnURL'] = $returnurl;        
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.

MB34
  • 4,210
  • 12
  • 59
  • 110
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)

Spider
  • 156
  • 2
  • 8
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.

Hotshot
  • 11
0

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

session_start();
0
    <!--First Line like as-->
 <?php session_start();?>
 <!-- Now, your php or html codes-->
<html>
  <head>
   .....
   .....
-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"]);
Rápli András
  • 3,869
  • 1
  • 35
  • 55