翻訳:スペイン語
Cookieを処理するためのPHPライブラリ。
オペレーティング システム: Linux。
PHP バージョン: 8.1 | 8.2 | 8.3.
この拡張機能をインストールするには、Composer を使用することをお勧めします。
php cookieライブラリをインストールするには、次のようにします。
composer require josantonius/cookie
前のコマンドは必要なファイルのみをインストールしますが、使用できるソース コード全体をダウンロードしたい場合は、次のようにします。
composer require josantonius/cookie --prefer-source
Git を使用して完全なリポジトリのクローンを作成することもできます。
git clone https://github.com/josantonius/php-cookie.git
JosantoniusCookieCookie
Cookie オプションを設定します。
/**
* 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
);
Cookie を名前で設定します。
/**
* @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 ;
複数の Cookie を一度に設定します。
/**
* 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 ;
名前で Cookie を取得します。
/**
* Optionally defines a default value when the cookie does not exist.
*/
public function get( string $ name , mixed $ default = null ): mixed ;
すべての Cookie を取得します。
public function all(): array ;
Cookie が存在するかどうかを確認します。
public function has( string $ name ): bool ;
Cookie を名前で削除し、その値を返します。
/**
* 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 ;
Cookie を名前で削除します。
/**
* @throws CookieException if headers already sent.
* @throws CookieException if failure in date/time string analysis.
*/
public function remove( string $ name ): void ;
JosantoniusCookieFacadesCookie
Cookie オプションを設定します。
/**
* 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 ;
Cookie を名前で設定します。
/**
* @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 ;
複数の Cookie を一度に設定します。
/**
* 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 ;
名前で Cookie を取得します。
/**
* Optionally defines a default value when the cookie does not exist.
*/
public static function get( string $ name , mixed $ default = null ): mixed ;
すべての Cookie を取得します。
public static function all(): array ;
Cookie が存在するかどうかを確認します。
public static function has( string $ name ): bool ;
Cookie を名前で削除し、その値を返します。
/**
* 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 ;
Cookie を名前で削除します。
/**
* @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 ;
このライブラリの使用例:
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 ' );
このライブラリのいくつかのメソッドで使用されるexpiresパラメータは、次の型を受け入れます: int|string|DateTime
。
ゼロを除くIntegers
UNIX 時間として処理されます。
Strings
日付/時刻形式として扱われます。詳細については、「サポートされている日付と時刻の形式」を参照してください。
$ cookie = new Cookie (
expires: ' 2016-12-15 +1 day '
);
これは次のようになります。
$ cookie = new Cookie (
expires: new DateTime ( ' 2016-12-15 +1 day ' )
);
DateTime
オブジェクトは UNIX 時刻を取得するために使用されます。
set
メソッドまたはreplace
メソッドで期限切れパラメータが使用されている場合は、Cookie オプションで設定された期限切れ値の代わりにこのパラメータが使用されます。
$ 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
オプションで渡されるexpiresパラメータが日付/時刻文字列の場合、オプションの設定時ではなく、 set
またはreplace
メソッドの使用時にフォーマットされます。
$ 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
テストを実行するには、composer を使用して以下を実行するだけです。
git clone https://github.com/josantonius/php-cookie.git
cd php-cookie
composer install
PHPUnit を使用して単体テストを実行します。
composer phpunit
PHPCS を使用してコード標準テストを実行します。
composer phpcs
PHP Mess Detector テストを実行して、コード スタイルの不一致を検出します。
composer phpmd
以前のすべてのテストを実行します。
composer tests
各リリースの詳細な変更点はリリース ノートに記載されています。
プルリクエストを作成したり、ディスカッションを開始したり、問題を報告したりする前に、必ず貢献ガイドをお読みください。
すべての貢献者に感謝します! ❤️
このプロジェクトが開発時間の短縮に役立つ場合は、私のオープンソース作業をサポートするスポンサーになっていただけますか?
このリポジトリは、MIT ライセンスに基づいてライセンスされています。
著作権 © 2016 年現在、ヨサントニウス