Übersetzungen : Español
PHP-Bibliothek zum Umgang mit Cookies.
Betriebssystem: Linux.
PHP-Versionen: 8.1 | 8,2 | 8.3.
Die bevorzugte Methode zur Installation dieser Erweiterung ist Composer.
Um php cookie -Bibliothek zu installieren, gehen Sie einfach wie folgt vor:
composer require josantonius/cookie
Mit dem vorherigen Befehl werden nur die erforderlichen Dateien installiert. Wenn Sie lieber den gesamten Quellcode herunterladen möchten, können Sie Folgendes verwenden:
composer require josantonius/cookie --prefer-source
Sie können das komplette Repository auch mit Git klonen :
git clone https://github.com/josantonius/php-cookie.git
JosantoniusCookieCookie
Legt Cookie-Optionen fest:
/**
* 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
);
Setzt ein Cookie nach Namen:
/**
* @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 ;
Setzt mehrere Cookies gleichzeitig:
/**
* 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 ;
Ruft ein Cookie nach Namen ab:
/**
* Optionally defines a default value when the cookie does not exist.
*/
public function get( string $ name , mixed $ default = null ): mixed ;
Ruft alle Cookies ab:
public function all(): array ;
Überprüfen Sie, ob ein Cookie vorhanden ist:
public function has( string $ name ): bool ;
Löscht ein Cookie nach Namen und gibt seinen Wert zurück:
/**
* 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 ;
Löscht ein Cookie nach Namen:
/**
* @throws CookieException if headers already sent.
* @throws CookieException if failure in date/time string analysis.
*/
public function remove( string $ name ): void ;
JosantoniusCookieFacadesCookie
Legt Cookie-Optionen fest:
/**
* 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 ;
Setzt ein Cookie nach Namen:
/**
* @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 ;
Setzt mehrere Cookies gleichzeitig:
/**
* 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 ;
Ruft ein Cookie nach Namen ab:
/**
* Optionally defines a default value when the cookie does not exist.
*/
public static function get( string $ name , mixed $ default = null ): mixed ;
Ruft alle Cookies ab:
public static function all(): array ;
Überprüfen Sie, ob ein Cookie vorhanden ist:
public static function has( string $ name ): bool ;
Löscht ein Cookie nach Namen und gibt seinen Wert zurück:
/**
* 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 ;
Löscht ein Cookie nach Namen:
/**
* @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 ;
Anwendungsbeispiel für diese Bibliothek:
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 ' );
Der in mehreren Methoden dieser Bibliothek verwendete Ablaufparameter akzeptiert die folgenden Typen: int|string|DateTime
.
Integers
außer Null werden als Unix-Zeit behandelt.
Strings
werden als Datums-/Uhrzeitformate behandelt. Weitere Informationen finden Sie unter Unterstützte Datums- und Uhrzeitformate.
$ cookie = new Cookie (
expires: ' 2016-12-15 +1 day '
);
Es wäre ähnlich wie:
$ cookie = new Cookie (
expires: new DateTime ( ' 2016-12-15 +1 day ' )
);
DateTime
Objekte werden verwendet, um die Unix-Zeit zu erhalten.
Wenn der Parameter „expires“ in den Methoden set
oder replace
verwendet wird, wird er anstelle des in den Cookie-Optionen festgelegten Werts „expires“ verwendet.
$ 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
Wenn der in den Optionen übergebene Ablaufparameter eine Datums-/Uhrzeitzeichenfolge ist, wird er bei Verwendung der set
oder replace
-Methode formatiert und nicht beim Festlegen der Optionen.
$ 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
Um Tests auszuführen, benötigen Sie lediglich Composer und führen Folgendes aus:
git clone https://github.com/josantonius/php-cookie.git
cd php-cookie
composer install
Führen Sie Unit-Tests mit PHPUnit durch:
composer phpunit
Führen Sie Code-Standardtests mit PHPCS durch:
composer phpcs
Führen Sie PHP Mess Detector-Tests aus, um Inkonsistenzen im Codestil zu erkennen:
composer phpmd
Führen Sie alle vorherigen Tests aus:
composer tests
Detaillierte Änderungen für jede Version sind in den Versionshinweisen dokumentiert.
Bitte lesen Sie unbedingt den Beitragsleitfaden, bevor Sie eine Pull-Anfrage stellen, eine Diskussion starten oder ein Problem melden.
Vielen Dank an alle Mitwirkenden! ❤️
Wenn Ihnen dieses Projekt hilft, Ihre Entwicklungszeit zu verkürzen, können Sie mich dann sponsern, um meine Open-Source-Arbeit zu unterstützen?
Dieses Repository ist unter der MIT-Lizenz lizenziert.
Copyright © 2016-heute, Josantonius