加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php – CodeIgniter会话set-cookie重复 – 如何解决

发布时间:2020-12-13 22:52:19 所属栏目:PHP教程 来源:网络整理
导读:更新: 我想知道是否有人可以查看我的答案,看看是否有任何漏洞. 在以下位置使用编码和会话时,有一个记录良好的问题: Duplicated “set-cookie: ci-session” fields in header by codeigniter 总之,每次调用set_userdata时,codeigniter都会执行set-cookie.
更新:

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

在以下位置使用编码和会话时,有一个记录良好的问题:

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

总之,每次调用set_userdata时,codeigniter都会执行set-cookie.

我找到了一个部分解决方案:

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

此解决方案的唯一问题是需要在任何地方插入代码.有没有一种简单的方法来清除所有标题?我已经修改了一些代码来删除php错误,但有没有办法可以使用钩子或其他东西?

<?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(),请仅使用此代码.如果你在控制器中使用echo,那么在删除标题之前会导致输出甚至被删除.

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

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

应用/钩/ 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,false);
        }
     }
}
?>

应用/配置/ hooks.php

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读