How do you set up use HttpOnly cookies in PHP

后端 未结 10 1768
终归单人心
终归单人心 2020-12-04 07:09

How can I set the cookies in my PHP apps as HttpOnly cookies?

相关标签:
10条回答
  • 2020-12-04 07:38

    For PHP's own session cookies on Apache:
    add this to your Apache configuration or .htaccess

    <IfModule php5_module>
        php_flag session.cookie_httponly on
    </IfModule>
    

    This can also be set within a script, as long as it is called before session_start().

    ini_set( 'session.cookie_httponly', 1 );
    
    0 讨论(0)
  • 2020-12-04 07:38

    Be aware that HttpOnly doesn't stop cross-site scripting; instead, it neutralizes one possible attack, and currently does that only on IE (FireFox exposes HttpOnly cookies in XmlHttpRequest, and Safari doesn't honor it at all). By all means, turn HttpOnly on, but don't drop even an hour of output filtering and fuzz testing in trade for it.

    0 讨论(0)
  • 2020-12-04 07:40

    The right syntax of the php_flag command is

    php_flag  session.cookie_httponly On
    

    And be aware, just first answer from server set the cookie and here (for example You can see the "HttpOnly" directive. So for testing delete cookies from browser after every testing request.

    0 讨论(0)
  • 2020-12-04 07:41
    <?php
    //None HttpOnly cookie:
    setcookie("abc", "test", NULL, NULL, NULL, NULL, FALSE); 
    
    //HttpOnly cookie:
    setcookie("abc", "test", NULL, NULL, NULL, NULL, TRUE); 
    
    ?>
    

    Source

    0 讨论(0)
提交回复
热议问题