Thilina
Thilina

Reputation: 117

Different session value on the other page

page 1

$formData = array();
$formData[0] = 'insert data done';
$_SESSION['form_data'] = $formData;
header('Location: view.php');

Page 2

if (isset($_SESSION['form_data'][0])) {
echo $_SESSION['form_data'][0];
unset($_SESSION['form_data']);
}

In the page 1 code, i comment out the header and put a

var_dump($_SESSION['form_data'][0]);

as what i was hope

"string(16) "insert data done"" 

this is correct but in the page 2 I put a

var_dump($_SESSION['form_data'][0]);

the output was

string(1) "i"

what I did wrong. in the page 2 i just want to echo the value and unset it but i tried to use

var_dump($_SESSION['form_data']);

the output was right

maybe my unsettling method might wrong.

Upvotes: 0

Views: 94

Answers (1)

Giona
Giona

Reputation: 21114

Have you tried echo $_SESSION['form_data'] ? it seems that you're getting the first letter of the string.

I think that if only one pair of key => value is set on a $_SESSION variable, it automatically reduces to its value. But i can't find any reference online.

Try to use this code on page 1:

$formData['someKey'] = 'insert data done';

and this on page 2:

echo $_SESSION['form_data']['someKey'];

let's see what happens...

Upvotes: 1

Related Questions