2012-05-09 61 views
11

我想知道如何在Codeigniter中将cookie设置为HTTPOnly。我查阅了文档,并没有看到如何设置这个标志。在codeigniter中使用HTTPOnly标志设置cookie

我也希望设置的cookie的安全标志,并发现它在config.php文件:

$config['cookie_secure'] = TRUE; 

不过,并没有在config.php的中HTTPOnly选项。

如何将所有Cookie设置为HTTPOnly?如果它在框架中完成,那么知道代码用于我自己的学习(以及默认设置是什么)会很有帮助。

回答

23

幸运的是,你可以在GitHub上

查看源代码 Session.php

在功能_set_cookie您将看到:

// Set the cookie 
setcookie(
    $this->sess_cookie_name, 
    $cookie_data, 
    $expire, 
    $this->cookie_path, 
    $this->cookie_domain, 
    $this->cookie_secure, 
    $this->cookie_httponly 
); 

$this->cookie_httponly的价值__construct指定,默认为false,但你可以设置它通过config.php TRUE如下:

$config['cookie_httponly'] = TRUE; 

这将enabl e在您的框架内的Cookie是HTTPOnly。

+0

谢谢你。接受和upvoted。我问过的好事。 – chowwy

+2

这不再适用于版本2.1。3在下面的评论中提到。 – ethicalhack3r

3

2.1.3在foreach中不包含cookie_httponly,所以它不会设置它。

foreach (array('sess_encrypt_cookie', 'sess_use_database', 'sess_table_name', 'sess_expiration', 'sess_expire_on_close', 'sess_match_ip', 'sess_match_useragent', 'sess_cookie_name', 'cookie_path', 'cookie_domain', 'cookie_secure', 'sess_time_to_update', 'time_reference', 'cookie_prefix', 'encryption_key') as $key) 

也不会在这里设置...

setcookie(
       $this->sess_cookie_name, 
       $cookie_data, 
       $expire, 
       $this->cookie_path, 
       $this->cookie_domain, 
       $this->cookie_secure 
      ); 
+0

我可以在我的测试中证实这一点,你知道它是如何设置的吗? – ethicalhack3r

+0

那么现在该怎么做? –

8

我发现自己是笨的会议MGMT一个drwaback因为

-

  1. 2.1 .3版本他们没有cookie_httponly标志。
    解决方案:
    第1步:您需要在库中创建自己的My_session.php并扩展系统会话文件或修改Session.php(在system-> library-> Session.php文件中)
    第2步:找到_set_cookie( $ cookie_data = NULL)功能,转到setcookie()函数,并修改为

    setcookie( $this->sess_cookie_name, $cookie_data, $expire,
    $this->cookie_path, $this->cookie_domain, $this->cookie_secure, TRUE ); // add True flag.

  2. 即使如果添加 “TRUE”,如果与OWASP ZAP工具测试,
    一段时间,它就会给你CSRF问题
    即:HTTPONLY不正确,
    安全标志未启用。

    原因:由于while sess_destroy他们将HTTPONLY和Secure标志设置为none。
    解决方案:在session.php文件更新sess_destroy功能

    // Kill the cookie 
    `setcookie(
          $this->sess_cookie_name, 
         addslashes(serialize(array())), 
         ($this->now - 31500000), 
         $this->cookie_path, 
         $this->cookie_domain, 
         TRUE, 
         TRUE 
        );` 
    

由于这些问题给了我寝食难安,所以我希望这将帮助你。 :)

相关问题