inapickle
inapickle

Reputation: 51

PHP Session Variables Lost Despite Consistent Session ID

Looking everywhere for an answer to this but no luck so far.

I've simplified my code as follows (running File 1 then File 2):

File 1

<?php
session_start();
echo session_id();
$_SESSION[123][123] = 'Testing';
echo '<pre>' . print_r($_SESSION, TRUE) . '</pre>';
?>

File 2

<?php
session_start();
echo session_id();
echo '<pre>' . print_r($_SESSION, TRUE) . '</pre>';
?>

The output from File 1 is the session ID and correctly showing the new session variable.

The output from File 2 is the same session ID but empty session variables.

Upvotes: 1

Views: 156

Answers (1)

Chilarai
Chilarai

Reputation: 1888

PHP variables cannot start with a number or any special character (except _). Hence, $_SESSION[123] or $_SESSION["123"] are all invalid

For the second part, to accept a 2D array in sessions, you have to do this

$some_array = array('123' => 'Testing');
$_SESSION['some_common_variable_name'] = $some_array;

Upvotes: 1

Related Questions