Traducciones : Español
Biblioteca PHP para el manejo de cookies.
Sistema operativo: Linux.
Versiones de PHP: 8.1 | 8.2 | 8.3.
La forma preferida de instalar esta extensión es a través de Composer.
Para instalar la biblioteca php cookie , simplemente:
composer require josantonius/cookie
El comando anterior solo instalará los archivos necesarios, si prefieres descargar el código fuente completo puedes usar:
composer require josantonius/cookie --prefer-source
También puedes clonar el repositorio completo con Git:
git clone https://github.com/josantonius/php-cookie.git
JosantoniusCookieCookie
Establece opciones de cookies:
/**
* Cookie options:
*
* domain: Domain for which the cookie is available.
* expires: The time the cookie will expire.
* httpOnly: If cookie will only be available through the HTTP protocol.
* path: Path for which the cookie is available.
* raw: If cookie will be sent as a raw string.
* sameSite: Enforces the use of a Lax or Strict SameSite policy.
* secure: If cookie will only be available through the HTTPS protocol.
*
* These settings will be used to create and delete cookies.
*
* @throws CookieException if $sameSite value is wrong.
*
* @see https://www.php.net/manual/en/datetime.formats.php for date formats.
* @see https://www.php.net/manual/en/function.setcookie.php for more information.
*/
public function __construct(
private string $ domain = '' ,
private int | string | DateTime $ expires = 0 ,
private bool $ httpOnly = false ,
private string $ path = ' / ' ,
private bool $ raw = false ,
private null | string $ sameSite = null ,
private bool $ secure = false
);
Establece una cookie por nombre:
/**
* @throws CookieException if headers already sent.
* @throws CookieException if failure in date/time string analysis.
*/
public function set(
string $ name ,
mixed $ value ,
null | int | string | DateTime $ expires = null
): void ;
Establece varias cookies a la vez:
/**
* If cookies exist they are replaced, if they do not exist they are created.
*
* @throws CookieException if headers already sent.
*/
public function replace(
array $ data ,
null | int | string | DateTime $ expires = null
): void ;
Obtiene una cookie por nombre:
/**
* Optionally defines a default value when the cookie does not exist.
*/
public function get( string $ name , mixed $ default = null ): mixed ;
Obtiene todas las cookies:
public function all(): array ;
Compruebe si existe una cookie:
public function has( string $ name ): bool ;
Elimina una cookie por nombre y devuelve su valor:
/**
* Optionally defines a default value when the cookie does not exist.
*
* @throws CookieException if headers already sent.
*/
public function pull( string $ name , mixed $ default = null ): mixed ;
Elimina una cookie por nombre:
/**
* @throws CookieException if headers already sent.
* @throws CookieException if failure in date/time string analysis.
*/
public function remove( string $ name ): void ;
JosantoniusCookieFacadesCookie
Establece opciones de cookies:
/**
* Cookie options:
*
* domain: Domain for which the cookie is available.
* expires: The time the cookie will expire.
* httpOnly: If cookie will only be available through the HTTP protocol.
* path: Path for which the cookie is available.
* raw: If cookie will be sent as a raw string.
* sameSite: Enforces the use of a Lax or Strict SameSite policy.
* secure: If cookie will only be available through the HTTPS protocol.
*
* These settings will be used to create and delete cookies.
*
* @throws CookieException if $sameSite value is wrong.
*
* @see https://www.php.net/manual/en/datetime.formats.php for date formats.
* @see https://www.php.net/manual/en/function.setcookie.php for more information.
*/
public static function options(
string $ domain = '' ,
int | string | DateTime $ expires = 0 ,
bool $ httpOnly = false ,
string $ path = ' / ' ,
bool $ raw = false ,
null | string $ sameSite = null ,
bool $ secure = false
): void ;
Establece una cookie por nombre:
/**
* @throws CookieException if headers already sent.
* @throws CookieException if failure in date/time string analysis.
*/
public static function set(
string $ name ,
mixed $ value ,
null | int | string | DateTime $ expires = null
): void ;
Establece varias cookies a la vez:
/**
* If cookies exist they are replaced, if they do not exist they are created.
*
* @throws CookieException if headers already sent.
*/
public static function replace(
array $ data ,
null | int | string | DateTime $ expires = null
): void ;
Obtiene una cookie por nombre:
/**
* Optionally defines a default value when the cookie does not exist.
*/
public static function get( string $ name , mixed $ default = null ): mixed ;
Obtiene todas las cookies:
public static function all(): array ;
Compruebe si existe una cookie:
public static function has( string $ name ): bool ;
Elimina una cookie por nombre y devuelve su valor:
/**
* Optionally defines a default value when the cookie does not exist.
*
* @throws CookieException if headers already sent.
*/
public static function pull( string $ name , mixed $ default = null ): mixed ;
Elimina una cookie por nombre:
/**
* @throws CookieException if headers already sent.
* @throws CookieException if failure in date/time string analysis.
*/
public static function remove( string $ name ): void ;
use Josantonius Cookie Exceptions CookieException ;
Ejemplo de uso de esta biblioteca:
use Josantonius Cookie Cookie ;
$ cookie = new Cookie ();
use Josantonius Cookie Facades Cookie ;
Cookie:: options ();
use Josantonius Cookie Cookie ;
$ cookie = new Cookie (
domain: ' example.com ' ,
expires: time () + 3600 ,
httpOnly: true ,
path: ' /foo ' ,
raw: true ,
sameSite: ' Strict ' ,
secure: true ,
);
use Josantonius Cookie Facades Cookie ;
Cookie:: options (
expires: ' now +1 hour ' ,
httpOnly: true ,
);
use Josantonius Cookie Cookie ;
$ cookie = new Cookie ();
$ cookie -> set ( ' foo ' , ' bar ' );
use Josantonius Cookie Facades Cookie ;
Cookie:: set ( ' foo ' , ' bar ' );
use Josantonius Cookie Cookie ;
$ cookie = new Cookie ();
$ cookie -> set ( ' foo ' , ' bar ' , time () + 3600 );
use Josantonius Cookie Facades Cookie ;
Cookie:: set ( ' foo ' , ' bar ' , new DateTime ( ' now +1 hour ' ));
use Josantonius Cookie Cookie ;
$ cookie = new Cookie ();
$ cookie -> replace ([
' foo ' => ' bar ' ,
' bar ' => ' foo '
]);
use Josantonius Cookie Facades Cookie ;
Cookie:: replace ([
' foo ' => ' bar ' ,
' bar ' => ' foo '
], time () + 3600 );
use Josantonius Cookie Cookie ;
$ cookie = new Cookie ();
$ cookie -> replace ([
' foo ' => ' bar ' ,
' bar ' => ' foo '
], time () + 3600 );
use Josantonius Cookie Facades Cookie ;
Cookie:: replace ([
' foo ' => ' bar ' ,
' bar ' => ' foo '
], time () + 3600 );
use Josantonius Cookie Cookie ;
$ cookie = new Cookie ();
$ cookie -> get ( ' foo ' ); // null if the cookie does not exist
use Josantonius Cookie Facades Cookie ;
Cookie:: get ( ' foo ' ); // null if the cookie does not exist
use Josantonius Cookie Cookie ;
$ cookie = new Cookie ();
$ cookie -> get ( ' foo ' , false ); // false if cookie does not exist
use Josantonius Cookie Facades Cookie ;
Cookie:: get ( ' foo ' , false ); // false if cookie does not exist
use Josantonius Cookie Cookie ;
$ cookie = new Cookie ();
$ cookie -> all ();
use Josantonius Cookie Facades Cookie ;
Cookie:: all ();
use Josantonius Cookie Cookie ;
$ cookie = new Cookie ();
$ cookie -> has ( ' foo ' );
use Josantonius Cookie Facades Cookie ;
Cookie:: has ( ' foo ' );
use Josantonius Cookie Cookie ;
$ cookie = new Cookie ();
$ cookie -> pull ( ' foo ' ); // null if attribute does not exist
use Josantonius Cookie Facades Cookie ;
Cookie:: pull ( ' foo ' ); // null if attribute does not exist
use Josantonius Cookie Cookie ;
$ cookie = new Cookie ();
$ cookie -> pull ( ' foo ' , false ); // false if attribute does not exist
use Josantonius Cookie Facades Cookie ;
Cookie:: pull ( ' foo ' , false ); // false if attribute does not exist
use Josantonius Cookie Cookie ;
$ cookie = new Cookie ();
$ cookie -> remove ( ' foo ' );
use Josantonius Cookie Facades Cookie ;
Cookie:: remove ( ' foo ' );
El parámetro expires utilizado en varios métodos de esta biblioteca acepta los siguientes tipos: int|string|DateTime
.
Integers
se manejarán como tiempo Unix excepto cero.
Strings
se manejarán como formatos de fecha/hora. Consulte Formatos de fecha y hora admitidos para obtener más información.
$ cookie = new Cookie (
expires: ' 2016-12-15 +1 day '
);
Sería similar a:
$ cookie = new Cookie (
expires: new DateTime ( ' 2016-12-15 +1 day ' )
);
Los objetos DateTime
se utilizarán para obtener la hora Unix.
Si el parámetro de caducidad se utiliza en los métodos set
o replace
, se tomará en lugar del valor de caducidad establecido en las opciones de cookies.
$ cookie = new Cookie (
expires: ' now +1 minute '
);
$ cookie -> set ( ' foo ' , ' bar ' ); // Expires in 1 minute
$ cookie -> set ( ' bar ' , ' foo ' , ' now +8 days ' ); // Expires in 8 days
$ cookie -> replace ([ ' foo ' => ' bar ' ]); // Expires in 1 minute
$ cookie -> replace ([ ' foo ' => ' bar ' ], time () + 3600 ); // Expires in 1 hour
Si el parámetro de caducidad pasado en las opciones es una cadena de fecha/hora, se formatea cuando se utiliza el método set
o replace
y no cuando se configuran las opciones.
$ cookie = new Cookie (
expires: ' now +1 minute ' , // It will not be formatted as unix time yet
);
$ cookie -> set ( ' foo ' , ' bar ' ); // It is will formatted now and expires in 1 minute
Para ejecutar pruebas solo necesitas Composer y ejecutar lo siguiente:
git clone https://github.com/josantonius/php-cookie.git
cd php-cookie
composer install
Ejecute pruebas unitarias con PHPUnit:
composer phpunit
Ejecute pruebas estándar de código con PHPCS:
composer phpcs
Ejecute pruebas de PHP Mess Detector para detectar inconsistencias en el estilo del código:
composer phpmd
Ejecute todas las pruebas anteriores:
composer tests
Los cambios detallados para cada versión están documentados en las notas de la versión.
Asegúrese de leer la Guía de contribución antes de realizar una solicitud de extracción, iniciar una discusión o informar un problema.
¡Gracias a todos los contribuyentes! ❤️
Si este proyecto le ayuda a reducir su tiempo de desarrollo, ¿puede patrocinarme para respaldar mi trabajo de código abierto?
Este repositorio tiene la licencia MIT.
Copyright © 2016-presente, Josantonio