🍪 bakes cookies for PSR-7 messages

Overview

PSR-7 Cookies

Latest Version on Packagist Supported PHP versions Total Downloads Software License

Install

Via Composer

$ composer require hansott/psr7-cookies

Usage

Add cookie to Psr\Http\Message\ResponseInterface



use HansOtt\PSR7Cookies\SetCookie;

// Set a cookie with custom values.
$cookie = new SetCookie('name', 'value', time() + 3600, '/path', 'domain.tld', $secure, $httpOnly, $sameSite);

// Set a cookie to delete a cookie.
$cookie = SetCookie::thatDeletesCookie('name');

// Set a cookie that stays forever (5 years)
$cookie = SetCookie::thatStaysForever('name', 'value');

// Set a cookie that expires at a given time.
$now = new DateTimeImmutable();
$tomorrow = $now->modify('tomorrow');
$cookie = SetCookie::thatExpires('name', 'value', $tomorrow);

// Add the cookie to a response
$responseWithCookie = $cookie->addToResponse($response);

Testing

$ composer test

Contributing

Please see CONTRIBUTING and CONDUCT for details.

Security

If you discover any security related issues, please email hans at iott consulting instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.

Comments
  • expires date format

    expires date format

    Hello,

    1. According to https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie and https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Date the date format whould be 'D, d M Y H:i:s T' instead of 'D, d-M-Y H:i:s T' I have seen (and used) both the formats, but I tend to follow MDN specs.

    2. Should we also be able to set the expiration dat using a cookie ttl/lifetime value instead of the actual expiry date/timestamp, so that both Expires and Max-Age can be added to the header? (btw, calculating Max-Age from timestamp would not be good as we would have to subtract current time() that in the (toHeaderValue)-call could accidentally yield an extra second since it was set) . Maybe a simple thatExpiresIn() method.

    kind regards, maks

    help wanted 
    opened by pine3ree 6
  • Removing cookie(s)

    Removing cookie(s)

    Hey! Thank you for the awesome package. Some questions;

    • Is there any way I could remove a cookie with this package?
    • (If not) Would you implement a method to remove a cookie?
    question 
    opened by wannfq 4
  • "Useless Functionality"

    Hi! I've been using this library for a while now. I'm curious - why remove the cookie signing and RequestCookies classes? That functionality is far from useless. We use RequestCookies to unpack cookies from a PSR-7 request ...

    opened by reznyt 4
  • Force cast cookie name to string

    Force cast cookie name to string

    Hello!

    First of all, I want to say thank you for your library! I'm using it on top of zend-expressive and someone is trying to hack the website by sending wrong request data (integer cookie name). So I see this error in logs:

    [0] TypeError: Argument 1 passed to HansOtt\PSR7Cookies\Cookie::__construct() must be of the type string, integer given, called in vendor/hansott/psr7-cookies/src/RequestCookies.php
    

    Is it possible to cast cookie name to string before creating Cookie instance?

    opened by spotman 3
  • Allow none as value for samesite

    Allow none as value for samesite

    Samesite=none is a valid value, however the validation does not currently allow this.

    This blog post highlights when you'd want to use this value: https://web.dev/samesite-cookie-recipes/ Cookies for cross-site usage must specify SameSite=None; Secure to enable inclusion in third party context.

    opened by Intrepidity 2
  • Array in cookies

    Array in cookies

    The Cookie class constructor accepts only strings as cookie $value, but there are cases where it could get an array. For example, if a cookie is sent like this:

    curl -v -k --cookie "array_name[0]=someValue" http://127.0.0.1:5000
    

    In this case, PHP is converting the cookie header value into an array; so, the ServerRequestInterface::getCookieParams() method returns this:

    Array
    (
        [array_name] => Array
            (
                [0] => someValue
            )
    
    )
    

    This causes RequestCookies::createFromRequest() to fail when creating a new Cookie() object for each cookie header set into the ServerRequestInterface object; that's because an array it's passed instead of a string, for argument 2. I would recommend to keep an eye on data sanitisation when fixing it, as this could possibly lead to code injection from malicious requests.

    opened by robertovernina 2
  • Improve API

    Improve API

    Currently it's kind of a PITA to set custom properties for cookies...

    e.g. you want to set a normal cookie with SameSite specified (or maybe combined with a expires in)

    Goals:

    • You can set individual parameters (with immutable API)
    • There are multiple ways to define time component: expires at, expires in, seconds valid, ... (This also needs to be testable, so a clock as dependency would be nice)
    • You cannot create an invalid cookie
    • It just works!
    • The current methods are preserved for backwards compat
    help wanted 
    opened by hansott 1
Releases(3.0.2)
  • 3.0.1(May 14, 2020)

  • 3.0.0(Apr 20, 2019)

    See https://github.com/hansott/psr7-cookies/issues/5#issuecomment-483602120

    Adding a cookie to a PSR-7 message is the only thing that this library should do, the other functionality is removed.

    How to upgrade?

    • RequestCookies -> $request->getCookieParams() gives you all request cookies in the format you're used to.
    • Signer -> I'm not sure what I should recommend at this point, I'm not an expert at security. That's part of the reason why I'm dropping the sign cookie functionality.
    • ResponseCookies -> Just use SetCookie->addToResponse

    Thanks for understanding.

    Hans

    Source code(tar.gz)
    Source code(zip)
  • 2.0.1(Feb 5, 2019)

  • 2.0.0(Mar 11, 2018)

    Although it's not an API change, users should be prepared to see exceptions for signed cookies.

    Signed cookies are now signed using the name AND value (previously only the value). This makes sure that a signed cookie cannot be renamed without changing the signature.

    Source code(tar.gz)
    Source code(zip)
  • 1.0.1(Jul 10, 2016)

    • Make sure cookie names are case insensitive in collections to prevent duplicates (c6b0001a6592c62021b887a1ac5e7e9fb54d4e8e)
    • Improved exception messages (cd830d5)
    • Improved test coverage (3bccfee)
    • RequestCookies & ResponseCookies now directly implement Iterator (before through CookieCollection interface), since only 2 downloads, no major/minor change. (c44b4ce)
    Source code(tar.gz)
    Source code(zip)
Owner
Hans Ott
Co-founder journy.io / Piano & guitar student / Avid skier / DDD / FP
Hans Ott
A small, modern, PSR-7 compatible PSR-17 and PSR-18 network library for PHP, inspired by Go's net package.

Net A small, modern, PSR-7 compatible PSR-17 and PSR-18 network library for PHP, inspired by Go's net package. Features: No hard dependencies; Favours

Minibase 16 Jun 7, 2022
A small, modern, PSR-7 compatible PSR-17 and PSR-18 network library for PHP, inspired by Go's net package.

Net A small, modern, PSR-7 compatible PSR-17 and PSR-18 network library for PHP, inspired by Go's net package. Features: No hard dependencies; Favours

Minibase 16 Jun 7, 2022
Learning about - Basic HTML & CSS, JSON, XML, Session & Cookies, CRUD Operations in Php using MySQL and Create MVC from scratch

This Project is based on course CSC 3215. Learning about - Basic HTML & CSS, JSON, XML, Session & Cookies, CRUD Operations in Php using MySQL and Create MVC (Model–View–Controller) from scratch. Just learning about web technologies, Not focusing on UI (Bootstrap or other 3rd-Party UI libraries or frameworks).

Alvi Hasan 5 Sep 21, 2022
A Simple MVC PHP Framework, integrated with lot of features such as Session, Cookies, Migrations, Database, Factories, Seeding, Bootstrap and Tailwind CSS

Navite A Simple MVC PHP Framework, integrated with lot of features such as Session, Cookies, Migrations, Database, Factories, Seeding, Bootstrap and T

Celionatti 2 Aug 22, 2022
This is a small piece of code to steal firefox's cookies and upload to server (Written in VBA)

Steal-firefox-cookies-VBA-macro This is a small piece of code to steal firefox's cookies and upload to server (Written in VBA) Please note For educati

HitmanAlharbi 6 Sep 27, 2022
Testing utilities for the psr/log package that backs the PSR-3 specification.

FIG - Log Test Testing utilities for the psr/log package that backs the PSR-3 specification. Psr\Log\Test\LoggerInterfaceTest provides a base test cla

PHP-FIG 3 Nov 19, 2022
jMQTT is a plugin for Jeedom aiming to connect Jeedom to an MQTT broker to subscribe and publish messages

jMQTT is a plugin for Jeedom aiming to connect Jeedom to an MQTT broker to subscribe and publish messages

null 19 Dec 27, 2022
PHP OOP interface for writing Slack Block Kit messages and modals

Slack Block Kit for PHP ?? For formatting messages and modals for Slack using their Block Kit syntax via an OOP interface ?? By Jeremy Lindblom (@jere

Slack PHP Framework 32 Dec 20, 2022
An easy code to send messages on a discord text channel with webhook.

Status Webhook-Discord An easy code to send messages on a discord text channel with webhook. Don't forget to check the latest version of Webhook-Disco

Victor 1 Dec 3, 2021
Sends log messages to the Logentries log management service

Phalcon Logentries Phalcon library to connect and make log entries using Logentries. You can adapt it to your own needs or improve it if you want. Ple

Phalcon Orphanage 10 Apr 15, 2019
Private groups to share messages, photos, videos, links with friends and family.

A truly private space for you and your friends What is Zusam ? Zusam (/tsuˈzam/) is a free and open-source way to self-host private forums for groups

Zusam 104 Dec 20, 2022
PHP-nats publisher - This is a simple package to publish messages to Nats

Nats publisher This is a simple package to publish messages to Nats Installation You can install the package using the Composer package manager. You c

Evert Jan Hakvoort 7 Oct 24, 2022
A simple PHP package for sending messages to Slack, with a focus on ease of use and elegant syntax.

Slack for PHP A simple PHP package for sending messages to Slack with incoming webhooks, focussed on ease-of-use and elegant syntax. Note: this packag

Regan McEntyre 1.2k Oct 29, 2022
A fully-managed real-time messaging service that allows you to send and receive messages between independent applications.

A fully-managed real-time messaging service that allows you to send and receive messages between independent applications.

Google APIs 58 Dec 23, 2022
Make WhatsApp ChatBot and use WhatsApp API to send the WhatsApp messages in php .

Ultramsg.com WhatsApp Bot using WhatsApp API and ultramsg Demo WhatsApp API ChatBot using Ultramsg API with php. Chatbot tasks: The output of the comm

Ultramsg 33 Nov 19, 2022
The best announcer for PocketMine-MP 4.0 servers, make messages for your users very easily

BroadcastACM The best announcer for PocketMine-MP 4.0 servers, make messages for your users very easily. Make the best announcements for your server w

fernanACM 3 May 30, 2022
[WIP] PSR-20 repository

[WIP] PSR Clock Work in Progress! This repository holds the interface for PSR-20. Note that this is not a clock of its own. It is merely an interface

PHP-FIG 46 Dec 30, 2022
PHP транслит с русского на латиницу с поддержкой PSR

PHP PSR Transliterator По-русски Особенности Транслит с русского на латиницу; Поддержка ISO9 вариант Б (ГОСТ 7.79—2000); Поддержка ЧПУ; Полное покрыти

null 1 Nov 25, 2021
The Cache component provides an extended PSR-6 implementation for adding cache to your applications.

Symfony PSR-6 implementation for caching The Cache component provides an extended PSR-6 implementation for adding cache to your applications. It is de

Symfony 3.8k Jan 3, 2023