duanmibei1929 2015-03-08 14:06
浏览 178

PHP session_set_cookie_params打破php session_start()函数和$ _SESSION变量

I'm trying to create a secure session by creating start_secure_session() function like this:

function start_secure_session() {
    $session_name   = 'secure_session';
    $secure         = true;
    $httponly       = true;

    if (ini_set('session.use_only_cookies', 1) === false) {
        die('error');
    }

    $cookie_params  = session_get_cookie_params();
    session_set_cookie_params($cookie_params["lifetime"],
        $cookie_params["path"],
        $cookie_params["domain"],
        $secure,
        $httponly);

    session_name($session_name);
    session_start();
    session_regenerate_id(true);
}

The problem is, start_secure_session() is not saving $_SESSION super global variable. It's unset immediately when the page refreshed. It works only if I comment this out:

    //$cookie_params    = session_get_cookie_params();
    //session_set_cookie_params($cookie_params["lifetime"],
    //  $cookie_params["path"],
    //  $cookie_params["domain"],
    //  $secure,
    //  $httponly);

or in other words, not setting a custom cookie params.

What should I do to make it works? So, the session_start() can work properly and $_SESSION variable can remember it's value?

  • 写回答

1条回答 默认 最新

  • dongsou3041 2017-03-03 15:26
    关注

    I think it is because of the secure=TRUE and you not using HTTPS? It will not create a session on a non-secure connection with secure set to TRUE.

    评论

报告相同问题?

悬赏问题

  • ¥15 关于大棚监测的pcb板设计
  • ¥20 sim800c模块 at指令及平台
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计