MB34
MB34

Reputation: 4404

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?

Upvotes: 9

Views: 42759

Answers (9)

Hemraj Patel
Hemraj Patel

Reputation: 51

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

Upvotes: 0

Afscell  Muhammed
Afscell Muhammed

Reputation: 139

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

session_start();

Upvotes: 1

R&#225;pli Andr&#225;s
R&#225;pli Andr&#225;s

Reputation: 3923

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"]);

Upvotes: -1

Hotshot
Hotshot

Reputation: 11

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.

Upvotes: 0

Spider
Spider

Reputation: 156

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)

Upvotes: 0

MB34
MB34

Reputation: 4404

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.

Upvotes: 0

Steampunk Forge
Steampunk Forge

Reputation: 106

i was able to get this to work like this

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

Upvotes: 2

George Cummins
George Cummins

Reputation: 28906

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'].

Upvotes: 3

DaneSoul
DaneSoul

Reputation: 4511

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

Upvotes: 13

Related Questions