当前位置: 动力学知识库 > 问答 > 编程问答 >

PHP Sessions Not Saving?

问题描述:

My PHP session variables don't seem to be saving page-to-page.

page1:

session_start();

$_SESSION['1'] = 'set';

var_dump($_SESSION);

RETURNS:

array(1) { [1]=> string(3) "set" }

Ok, so the session variable is set. Now if I goto ...

page 2:

session_start();

if ($_SESSION['1'] == 'set'){

echo "set";

}

elseif($_SESSION['1'] !== 'set'){

echo "Not set";

}

else die("error");

echo "<br />";

var_dump($_SESSION);

RETURNS:

Not set

array(0) { }

Any ideas where my session variable is going???

Many thanks.

网友答案:

Regarding array keys, from http://php.net/manual/en/language.types.array.php, "Strings containing valid integers will be cast to the integer type".

The manual on $_SESSION says "An associative array". So an associative array is expected literally...? It does no one any good if this bit of important info about accessing and storing session data remains buried in manual comments.

Session variables with a single number will not work, however "1a" will work, as will "a1" and even a just single letter, for example "a" will also work.

(Invalid) 1st page

<?php
session_start();
$_SESSION["1"] = "LOGGED";
?>

2nd page

<?php
session_start();
echo $_SESSION["1"];
?>

(Valid) 1st page

<?php
session_start();
$_SESSION["a"] = "LOGGED";
?>

2nd page

<?php
session_start();
echo $_SESSION["a"];
?>

(Valid) 1st page

<?php
session_start();
$_SESSION["a1"] = "LOGGED";
?>

2nd page

<?php
session_start();
echo $_SESSION["a1"];
?>

Example from PHP.net manual on Session variables

<?php
$_SESSION[1][1] = 'cake'; // fails

$_SESSION['v1'][2] = 'cake'; // works
?>

Source: http://php.net/manual/en/language.types.array.php

网友答案:
 use
 replace 
 $_SESSION['1'] = 'set';   

 to
 $_SESSION['abc'] = 'set';
分享给朋友:
您可能感兴趣的文章:
随机阅读: