doutuo6048 2012-09-11 09: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 09: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.

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

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部