dsa5211314 2012-09-14 12:44
浏览 41
已采纳

对象会话未被传递

On File A.php I have a while Loop that pulls data from a database and connects to class.php to create objects for each dataset. That part works fine.
Within that Loop I try to save each object to a SESSION defined by its id value after New Object has been created

File A.php:

$_SESSION[$pObject->id] = $pObject;

if (isset($_SESSION[$pObject->id]))

{

echo "SESSION $pObject->id is set"; 

}

I have confirmed that it IS being created.

NOW, I have a form that sends an Objects id value via a GET to File B.php, I have confirmation that the value is received.

File B.php:

require_once 'class.php';

session_start();

$id = $_GET['id'];

echo $id;

//Now $id is in my new file, so I try to call my SESSION

if(isset($_SESSION[$id]))

{

echo "SESSION $id is set";

$pObjectCurrrent = $_SESSION[$id];

}

else{

echo "SESSION $id is Not set";

}

The issue is, that despite $id in File B.php being equal to the value of $pObject->id in File A.php the SESSION[$id] in File B.php is NOT set and has no value. Instead I receive an undefined variable error. Any ideas would be greatly appreciated.

  • 写回答

1条回答 默认 最新

  • dongmanni6916 2012-09-14 13:07
    关注

    You can't use an integer as your session variable name.

    You could set the session var like this:

    $_SESSION['obj' . $pObject->id] = $pObject;
    

    Then retrieve it from B.php like this:

    echo $_SESSION['obj' . $id];
    

    Essentially that concatenates the ID with a string so the variable name would be "obj5", "obj2", etc.

    Also - if you're passing a lot of data in session objects, I'd recommend researching serializing and alternatives like storing session data to DB.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 请问如何在openpcdet上对KITTI数据集的测试集进行结果评估?
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路
  • ¥15 phython读取excel表格报错 ^7个 SyntaxError: invalid syntax 语句报错