2012-11-05 38 views
0

Possible Duplicate:
PHP: Notice: Undefined index where the session variable is defined
PHP: “Notice: Undefined variable” and “Notice: Undefined index”PHP公告:未定义的索引错误与会话

好了,这是错误:

Notice: Undefined index: overview_id in C:\xampp\htdocs\site\home.php on line 6

这是我的代码:

<?php session_start(); 

    $sess = $_SESSION["overview_id"]; 

    if(isset($sess)){ 
    header("Location: account.php"); 
    } 

?> 

任何帮助将是很酷。

谢谢。

+0

$ _SESSION [ “overview_id”]没有定义。就像错误信息所示 – 2012-11-05 23:31:48

+1

这不是一个错误,它是一个*通知*,它只是告诉你一些不错的做法 –

+0

我不明白它:如果你检查'overview_id'元素是否被定义(你用'isset'来检查它),为什么你要创建一个临时变量,给它赋值,然后检查这个变量?为什么不只是'if(isset($ _ SESSION ['overview_id']))'然后呢?我的意思是,这个逻辑背后的推理是什么? – raina77ow

回答

0

你的第三个行只是改变

if (isset($_SESSION['overview_id'])){ 

相反,并删除第二行。

0

您设置SESSION_ID前检查,所以你设置到另一个变量之前始终必须至少有默认值分配的,所以最好使用此

if(isset($_SESSION["overview_id"])){ 
    header("Location: account.php"); 
} 
0

在您所提供的代码的条款:

<?php 
session_start(); 

if(isset($_SESSION["overview_id"])){ 
    header("Location: account.php"); 
} 

?> 

这将解决该问题

0

因为$_SESSION["overview_id"]没有设置,那么$sess = $_SESSION["overview_id"];将失败。这是你如何做你的测试:

<?php session_start(); 

    if (isset($_SESSION["overview_id"])) { 
     header("Location: account.php"); 
    } 
?>