Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Session variable not getting set

Tags:

php

session

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?

like image 861
MB34 Avatar asked May 23 '12 17:05

MB34


2 Answers

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

like image 182
DaneSoul Avatar answered Sep 22 '22 18:09

DaneSoul


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

like image 35
George Cummins Avatar answered Sep 21 '22 18:09

George Cummins