แปล : Español
ไลบรารี PHP สำหรับจัดการคุกกี้
ระบบปฏิบัติการ: ลินุกซ์
เวอร์ชัน 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 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
);
ตั้งค่าคุกกี้ตามชื่อ:
/**
* @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 ;
ตั้งค่าคุกกี้หลายรายการพร้อมกัน:
/**
* 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 ;
รับคุกกี้ตามชื่อ:
/**
* Optionally defines a default value when the cookie does not exist.
*/
public function get( string $ name , mixed $ default = null ): mixed ;
รับคุกกี้ทั้งหมด:
public function all(): array ;
ตรวจสอบว่ามีคุกกี้อยู่หรือไม่:
public function has( string $ name ): bool ;
ลบคุกกี้ตามชื่อและส่งคืนค่า:
/**
* 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 ;
ลบคุกกี้ตามชื่อ:
/**
* @throws CookieException if headers already sent.
* @throws CookieException if failure in date/time string analysis.
*/
public function remove( string $ name ): void ;
JosantoniusCookieFacadesCookie
ตั้งค่าตัวเลือกคุกกี้:
/**
* 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 ;
ตั้งค่าคุกกี้ตามชื่อ:
/**
* @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 ;
ตั้งค่าคุกกี้หลายรายการพร้อมกัน:
/**
* 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 ;
รับคุกกี้ตามชื่อ:
/**
* Optionally defines a default value when the cookie does not exist.
*/
public static function get( string $ name , mixed $ default = null ): mixed ;
รับคุกกี้ทั้งหมด:
public static function all(): array ;
ตรวจสอบว่ามีคุกกี้อยู่หรือไม่:
public static function has( string $ name ): bool ;
ลบคุกกี้ตามชื่อและส่งคืนค่า:
/**
* 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 ;
ลบคุกกี้ตามชื่อ:
/**
* @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 ' );
พารามิเตอร์ หมดอายุ ที่ใช้ในหลายวิธีของไลบรารีนี้ยอมรับประเภทต่อไปนี้: int|string|DateTime
Integers
จะถูกจัดการเป็นเวลายูนิกซ์ยกเว้นศูนย์
Strings
จะถูกจัดการเป็นรูปแบบวันที่/เวลา ดูรูปแบบวันที่และเวลาที่รองรับสำหรับข้อมูลเพิ่มเติม
$ cookie = new Cookie (
expires: ' 2016-12-15 +1 day '
);
มันจะคล้ายกับ:
$ cookie = new Cookie (
expires: new DateTime ( ' 2016-12-15 +1 day ' )
);
วัตถุ DateTime
จะถูกนำมาใช้เพื่อรับเวลายูนิกซ์
หากใช้พารามิเตอร์ หมดอายุ ใน set
หรือ replace
วิธีการนั้นจะถูกนำไปใช้แทนค่า หมดอายุ ที่ตั้งไว้ในตัวเลือกคุกกี้
$ 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
หากพารามิเตอร์ หมดอายุ ที่ส่งในตัวเลือกเป็นสตริงวันที่/เวลา พารามิเตอร์นั้นจะถูกจัดรูปแบบเมื่อใช้วิธี 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
หากต้องการรันการทดสอบ คุณเพียงแค่ต้องมีผู้แต่งและดำเนินการดังต่อไปนี้:
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-ปัจจุบัน, Josantonius