php 警告会话变量,注意:未定义的索引:


php warning session variable, Notice: Undefined index:

if($_SESSION["adm"]==NULL)
   header("location:signin.php");

通过这段代码,我想检查会话变量是否为空。首次加载主页时,会话变量通常为 null。 如果会话变量为 null,则页面应重定向到Login页面,否则它将保留在Home页面本身中。

错误 ->>>注意:未定义的索引:adm in....

分配任何值后,此警告永远不会显示。

仅在设置后尝试使用SESSION,或执行类似操作;

if( array_key_exists('adm', $_SESSION) ) {
  //It exists
} else {
  //It doesn't exist
}

或者使用三元运算符并设置默认值 - 不过,这是一种"黑客"的方式。

$_SESSION['adm'] = array_key_exists('adm', $_SESSION) ? $_SESSION['adm'] : '';

1(确保在PHP页面的开头包含session_start()

2( 始终尝试访问变量SESSION仅当它设置了值时。

if(isset($_SESSION['adm']))
{
   // perform the operations
}