문제

I'm sure this is just something I've overlooked, but I'm struggling a bit to work it out.

I've two simple php pages:

sesh1.php

<?php
session_start();
echo session_id();

//create array
$survey = array(
    'question 1' => array('horse', 'cow','pig'),
    'question 2' => 12,
    'question 3' => 'Man',
    'question 4' => 'Woman'

);

//add the array to the session
$_SESSION[]=serialize($survey);


//show session contents
print_r($_SESSION);

Results:
c10a65902644c193496fc0292f4c13b1
Array ( [0] => a:4:{s:10:"question 1";a:3:{i:0;s:5:"horse";i:1;s:3:"cow";i:2;s:3:"pig";}s:10:"question 2";i:12;s:10:"question 3";s:3:"Man";s:10:"question 4";s:5:"Woman";} )

sesh2.php

<?php
session_start();
echo session_id();
//show the session
print_r($_SESSION);

Results:
c10a65902644c193496fc0292f4c13b1
Array ( )

I can't work out why a blank array is being returned when I navigate from sesh1 to sesh2. As you can see, the session ID is preserved, so why not the session contents? All my php.ini settings seem to be what would be expected for working sessions.

Can anyone advise what I'm missing?

도움이 되었습니까?

해결책

You need a key in your session variable...

$_SESSION['survey']=serialize($survey);

print_r($_SESSION['survey']);

Also you don't need to serialize your array, it will maintain the array for you.

다른 팁

Try saving to an actual named variable rather than trying to append it to the end of the array.

$_SESSION['survey'] = json_encode();
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top