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 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私