2012-05-09 59 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 
        );` 
    

由於這些問題給了我寢食難安,所以我希望這將幫助你。 :)

相關問題