Cómo configurar el uso de cookies HttpOnly en PHP


¿Cómo puedo configurar las cookies en mi PHP apps como HttpOnly cookies?

Author: Alive to Die, 2008-08-31

9 answers

  • Para sus cookies, consulte esta respuesta.
  • Para La propia cookie de sesión de PHP (PHPSESSID, por defecto), ver @ richie's answer

El setcookie() y setrawcookie() funciones, introdujo el parámetro httponly, de vuelta en la edad oscura de PHP 5.2.0, haciendo esto agradable y fácil. Simplemente establezca el parámetro 7 a true, según la sintaxis

Sintaxis de función simplificada para brevedad

setcookie(    $name, $value, $expire, $path, $domain, $secure, $httponly )
setrawcookie( $name, $value, $expire, $path, $domain, $secure, $httponly )

Enter NULL for parámetros que desea mantener por defecto. También puede considerar si debe configurar el parámetro secure.

También es posible usar el más antiguo, de nivel inferiorheader() función:

header( "Set-Cookie: name=value; httpOnly" );
 78
Author: Cheekysoft,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-05-23 12:03:06

Para las propias cookies de sesión de PHP en Apache:
agregue esto a su configuración de Apache o .htaccess

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

Esto también se puede establecer dentro de un script, siempre y cuando se llame antes de session_start().

ini_set( 'session.cookie_httponly', 1 );
 99
Author: richie,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-12-30 17:06:24

Tenga en cuenta que HttpOnly no detiene el cross-site scripting; en su lugar, neutraliza un posible ataque, y actualmente lo hace solo en IE (FireFox expone cookies HttpOnly en XMLHttpRequest, y Safari no lo respeta en absoluto). Por todos los medios, encienda HttpOnly, pero no pierda ni una hora de filtrado de salida y pruebas de fuzz en el comercio por él.

 13
Author: tqbf,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2008-09-10 21:40:41

Tenga en cuenta que las cookies de sesión PHP no utilizan httponly por defecto.

Para hacer eso:

$sess_name = session_name();
if (session_start()) {
    setcookie($sess_name, session_id(), null, '/', null, null, true);
}

Un par de puntos de nota aquí:

  • Tienes que llamar session_name() antes session_start()
  • Esto también establece la ruta predeterminada a'/', que es necesario para Opera pero que PHP las cookies de sesión no funcionan de forma predeterminada bien.
 11
Author: ,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2008-10-30 14:57:19

Explicación aquí de Ilia... 5.2 aunque

Compatibilidad con HttpOnly cookie flag en PHP 5.2

Como se indica en ese artículo, puede establecer el encabezado usted mismo en versiones anteriores de PHP

header("Set-Cookie: hidden=value; httpOnly");
 5
Author: Polsonby,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2008-08-31 14:35:02
<?php
//None HttpOnly cookie:
setcookie("abc", "test", NULL, NULL, NULL, NULL, FALSE); 

//HttpOnly cookie:
setcookie("abc", "test", NULL, NULL, NULL, NULL, TRUE); 

?>

Fuente

 5
Author: Marius,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2008-08-31 14:36:36

Puede especificarlo en la función set cookie ver el manual de php

setcookie('Foo','Bar',0,'/', 'www.sample.com'  , FALSE, TRUE);
 5
Author: Re0sless,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2008-08-31 14:37:06

Puede usar esto en un archivo de encabezado.

// setup session enviroment
ini_set('session.cookie_httponly',1);
ini_set('session.use_only_cookies',1);

De esta manera todas las futuras cookies de sesión usarán httponly.

  • Actualizado.
 3
Author: Marius,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-06-23 22:24:21

La sintaxis correcta del comando php_flag es

php_flag  session.cookie_httponly On

Y tenga en cuenta, solo la primera respuesta del servidor establecer la cookie y aquí (por ejemplo, se puede ver la directiva "HttpOnly". Por lo tanto, para probar, elimine las cookies del navegador después de cada solicitud de prueba.

 1
Author: Mareg,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-11-19 20:51:10