doutuo6048 2012-09-11 17:13
浏览 56
已采纳

对于会话变量的未定义索引,即使我已经设置了cookie

I am trying to set cookies to a php session variable so that a session does not expire for 2 months. The problem I am getting though is that after about 3 hours, when I refresh the page, I get a undefined index for $_SESSION['id'] in line 30. But I don't get the undefined index for $_SESSION['id'] in between lines 20-23.

Why am I still getting undefined indexes even though I have set cookies for this session variable?

$inTwoMonths = 60 * 60 * 24 * 60 + time();
    setcookie('id', 'ID', $inTwoMonths);


    //line 20-23
    if (isset($_POST['id'])) {

    $_SESSION['id'] = $_POST['id'];

    }

    //line 30
    <?php echo $_SESSION['id'] ?>
  • 写回答

1条回答 默认 最新

  • dspld86684 2012-09-11 17:29
    关注

    SESSION != COOKIE.

    You must set SESSION TTL in php.ini (20min default).

    On line 20-23 you dont get undefined index, coz you setting var. But on line 30 you trying to read undefined variable(coz you dont set-up it before.)

    Try something like this:

    $_SESSION['id'] = $_COOKIE['id'];
    

    Also you need use session_start(); to init session.

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

报告相同问题?

悬赏问题

  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?