php - CodeIgniter session set-cookie duplicated -- 如何解决

标签 php codeigniter session cookies

更新:

我想知道是否有人可以查看我的答案,看看其中是否有任何漏洞。

在以下位置使用 codeingiter 和 session 时有一个记录良好的问题:

Duplicated "set-cookie: ci-session" fields in header by codeigniter

总而言之,每次调用 set_userdata 时,codeigniter 都会设置 cookie。

我在以下位置找到了部分解决方案:

http://ha17.com/1745-bigip-f5-header-max-size-collides-with-codeigniters-bizarre-session-class/

此解决方案的唯一问题是需要在任何地方插入代码。有没有一种简单的方法可以清除所有标题?我已经稍微修改了代码以消除 php 错误,但是有什么方法可以使用 Hook 或其他东西吗?

<?php
class MY_Controller extends CI_Controller
{

    public function __construct()
    {
        parent:: __construct();
    }

     //See (modified from) http://ha17.com/1745-bigip-f5-header-max-size-collides-with-codeigniters-bizarre-session-class/
    protected function _removeDuplicateCookieHeaders ()
    {
        // clean up all the cookies that are set...
        $headers             = headers_list();
        $cookies_to_output   = array ();
        $header_session_cookie = '';
        $session_cookie_name = $this->config->item('sess_cookie_name');

        foreach ($headers as $header)
        {
            list ($header_type, $data) = explode (':', $header, 2);
            $header_type = trim ($header_type);
            $data        = trim ($data);

            if (strtolower ($header_type) == 'set-cookie')
            {
                header_remove ('Set-Cookie'); 

                $cookie_value = current(explode (';', $data));
                list ($key, $val) = explode ('=', $cookie_value);
                $key = trim ($key);

                if ($key == $session_cookie_name)
                {
                   // OVERWRITE IT (yes! do it!)
                   $header_session_cookie = $data;
                   continue;
                } 
                    else 
                    {
                   // Not a session related cookie, add it as normal. Might be a CSRF or some other cookie we are setting
                   $cookies_to_output[] = array ('header_type' => $header_type, 'data' => $data);
                }
            }
        }

        if ( ! empty ($header_session_cookie))
        {
            $cookies_to_output[] = array ('header_type' => 'Set-Cookie', 'data' => $header_session_cookie);
        }

        foreach ($cookies_to_output as $cookie)
        {
            header ("{$cookie['header_type']}: {$cookie['data']}", false);
        }
     }
}

最佳答案

编辑:仅当您使用 $this->load->view() 时才使用此代码。如果您在 Controller 中使用 echo 权利,这将导致在删除 header 之前输出甚至被删除。

编辑需要 php 5.3 或更新版本。

我找到了一种方法,我认为我可以帮助其他人解决这个问题。我还没有完美地测试它,但它似乎有效。

应用程序/ Hook /session_cookie_fixer.php

<?php
class SessionCookieFixer
{   
     //See (modified from) http://ha17.com/1745-bigip-f5-header-max-size-collides-with-codeigniters-bizarre-session-class/
    function removeDuplicateSessionCookieHeaders ()
    {
         $CI = &get_instance();

        // clean up all the cookies that are set...
        $headers             = headers_list();
        $cookies_to_output   = array ();
        $header_session_cookie = '';
        $session_cookie_name = $CI->config->item('sess_cookie_name');

        foreach ($headers as $header)
        {
            list ($header_type, $data) = explode (':', $header, 2);
            $header_type = trim ($header_type);
            $data        = trim ($data);

            if (strtolower ($header_type) == 'set-cookie')
            {
                header_remove ('Set-Cookie'); 

                $cookie_value = current(explode (';', $data));
                list ($key, $val) = explode ('=', $cookie_value);
                $key = trim ($key);

                if ($key == $session_cookie_name)
                {
                   // OVERWRITE IT (yes! do it!)
                   $header_session_cookie = $data;
                   continue;
                } 
                    else 
                    {
                   // Not a session related cookie, add it as normal. Might be a CSRF or some other cookie we are setting
                   $cookies_to_output[] = array ('header_type' => $header_type, 'data' => $data);
                }
            }
        }

        if ( ! empty ($header_session_cookie))
        {
            $cookies_to_output[] = array ('header_type' => 'Set-Cookie', 'data' => $header_session_cookie);
        }

        foreach ($cookies_to_output as $cookie)
        {
            header ("{$cookie['header_type']}: {$cookie['data']}", false);
        }
     }
}
?>

应用程序/配置/hooks.php

$hook['post_controller'][] = array(
                               'class'    => 'SessionCookieFixer',
                               'function' => 'removeDuplicateSessionCookieHeaders',
                               'filename' => 'session_cookie_fixer.php',
                               'filepath' => 'hooks',
                               'params'   => array()
                               );

关于php - CodeIgniter session set-cookie duplicated -- 如何解决,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23255854/

相关文章:

javascript - TypeError : oTable. fnReloadAjax 不是函数 - DataTables

session - Tomcat 处理 JRuby/Rails 应用程序上的 session

php - 帮助在不更改当前页面的情况下更改页面语言(PHP)

java - 使 session 无效和 request.getsession(true) 后 JSession ID 保持不变

php - 使用本地主机从 MYSQL 更改为 MYSQLI 无法正常工作

php - 内联三元运算符不工作

php - 搜索结果显示类似 google php

php - CodeIgniter-形成警告消息?

php - session 已在 FOS User Bundle Registration Confirmation 中开始

php - 在 MySQL 查询中的 where 子句中传递变量 PHP CodeIgniter