Flurin Juvalta
Flurin Juvalta

Reputation: 91

PHP does not store all variables in session

I'm assigning session variables by filling the $_SESSION - Array throughout my script. My problem is, that for some reason not all variables are available in the session.

here is a shortened version of my code for explaining this issue:

session_start();

print_r($_SESSION);

$_SESSION['lang']        = 'de';
$_SESSION['location_id'] = 11;
$_SESSION['region_id']   = 1;

$_SESSION['userid'] = 'eccbc87e4b5ce2fe28308fd9f2a7baf3';
$_SESSION['hash']   = 'dce57f1e3bc6fba32afab93b0c38b662';

print_r($_SESSION);

first call prints something like this:

Array
(
)
Array
(
    [lang] => de
    [location_id] => 11
    [region_id] => 1
    [userid] => eccbc87e4b5ce2fe28308fd9f2a7baf3
    [hash] => dce57f1e3bc6fba32afab93b0c38b662
)

the second call prints:

Array
(
    [lang] => de
    [location_id] => 11
    [region_id] => 1
)
Array
(
    [lang] => de
    [location_id] => 11
    [region_id] => 1
    [userid] => eccbc87e4b5ce2fe28308fd9f2a7baf3
    [hash] => dce57f1e3bc6fba32afab93b0c38b662
)

As you can see, the important login information is not stored in the session. Does anybody has an idea what could be wrong with my session? Thanks for your answers!

Upvotes: 2

Views: 346

Answers (2)

Dan H
Dan H

Reputation: 3623

Paste this code, untouched, in a single script and run it several times. You should get the same results the 2nd, 3rd, 4th... time.

<?php
session_start();

print_r($_SESSION);

$_SESSION['lang']        = 'de';
$_SESSION['location_id'] = 11;
$_SESSION['region_id']   = 1;

$_SESSION['userid'] = 'eccbc87e4b5ce2fe28308fd9f2a7baf3';
$_SESSION['hash']   = 'dce57f1e3bc6fba32afab93b0c38b662';

print_r($_SESSION);
?>

If it works, then you obviously have something wrong in your script that you are not posting. In that case you should provide more code in order to be able to help you.

Upvotes: 0

John Conde
John Conde

Reputation: 219874

Further expanding on what Pekka might be alluding to, if you have register globals on there may be a naming conflict with your session variables and other variables in your script. If possible turn register globals off or rename your variables they don't collide ($_SESSION'hash'] and $hash) and see what happens.

Upvotes: 1

Related Questions