php变量设置但无法访问:注意:未定义的索引:

时间:2018-04-05 19:14:37

标签: php session-variables

我已设置会话数据,似乎设置为var_dump($_SESSION["favfilms"]["title"])建议返回一个包含所有正确会话数据的大量字符串。但是当我通过指定echo $_SESSION["favfilms"]["title"]来输出数据时,我得到一个未定义索引的错误?

以下是设置会话变量的代码

<?php
ob_start();
session_start();
$category = $_POST["category"];

$_SESSION["fav$category"]["title"] = $_POST["title"];
$_SESSION["fav$category"]["body"] = $_POST["body"];
$_SESSION["fav$category"]["img"] = $_POST["img"];
$_SESSION["fav$category"]["category"] = $_POST["category"];
$_SESSION["fav$category"]["id"] = $_POST["id"];   

?>

以下是我将数据呈现给视图的代码

var_dump($_SESSION);
//prints all the session data in one long array structured string
if(isset($_SESSION["favfilms"])){
  echo $_SESSION["favfilms"]["title"] ."<br>";
  echo $_SESSION["favfilms"]["body"]."<br>";
  echo $_SESSION["favfilms"]["img"]."<br>";
  echo $_SESSION["favfilms"]["category"]."<br>";
}else{
  echo "Session fav not set";
  echo $_SESSION["favfilms"]["title"];
}

如何访问会话数据?

这是它返回我的所有会话数据的字符串

array(3) { 
        ["username"]=> string(4) "mfal" 
        ["password"]=> string(3) "pwd" 
        ["favfilms "] => array(5) { 
                    ["title"]=> string(10) "Epic Film!" 
                    ["body"]=> string(449) "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat merpproident, merpproidentmerpproident merpproident mollit anim id est laborum." 
                    ["img"]=> string(49) "http://localhost:4000/reviews.com/img/entebbe.jpg" 
                    ["category"]=> string(6) "films " 
                    ["id"]=> string(1) "3" 
                    } 
        } 

会话赞不设置

0 个答案:

没有答案