الترجمات : الاسبانية
مكتبة 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
للحصول على وقت Unix.
إذا تم استخدام معلمة انتهاء الصلاحية في أساليب 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 إلى الوقت الحاضر، جوسانتونيوس