views:

38

answers:

3

I'm trying to set a session variable and use it on another page.

I have: pg1

session_start();
$_session['sessionID'] = $row['ID'];

Then on page two I have.

session_start();
$userID = $sessionID;

But when I use JC to alert this out I get nothing.. Am I doing this wrong? Thanks.

+6  A: 

Rather than:

$userID = $sessionID;

Use:

$userID = $_SESSION['sessionID']

You need to specify the $_SESSION there because that is the array you stored the value in :)

Have a look at this session tutorial if you want.

Sarfraz
It's case sensitive, so it needs to be `$_SESSION`
ircmaxell
@ircmaxell: Updated before seeing your comment :)
Sarfraz
And don't forget to check (where necessary) if the session variable is set using something like `if (!isset($_SESSION['sessionID'])) { header('Location: /login.php') }`
Tom
@Tom: That is up to OP if really wants to redirect like you assumed or do something else :)
Sarfraz
Thanks I'll try that. Also what is the use of session_register() ?
creocare
@creocare: It register the global variables with the current session, you might want to have a look at the docs for example usage :)
Sarfraz
@Sarfraz: That is why I posted this as a comment. It is obvious that @creocare is new to programming so I thought it was an important thing to point out this simple and effective way of securing most php web applications
Tom
+1  A: 

On the second page, you'll need to say

$userID = $_SESSION['sessionID'];
codersarepeople
+1  A: 

You need to do the following on page 2:

session_start();
$userID = $_SESSION['sessionID'];

You also need to use $_SESSION, not $_session

Rupert