php $_SESSION variable doesn’t stay persistent
Feb.15, 2008 in
Development
Recently I was creating a new website from scratch, which required authentication and the use of PHP’s sessions functionality. I used the session to store information which I needed to be persistent between pages:
Page 1
session_start();
$_SESSION['User_ID'] = '123';
....
//redirect to page 2
header('Location: page2.php');
?>
Page 2
print "User ID is: ".$_SESSION['User_ID'];
?>
However page 2 would never display the user ID value. I couldn’t work out what was causing the problem. The solution is to make sure you include session_start() at the top of ever page you need to access the $_SESSION super global array.
Tags: Php

Leave a Reply