codezero-be / cookie by codezero

Your friendly, furry cookie monster. Get and set cookies with ease.
37,268
9
4
Package Data
Maintainer Username: codezero
Maintainer Contact: ivan@codezero.be (Ivan Vermeyen)
Package Create Date: 2015-03-28
Package Last Update: 2019-09-27
Language: PHP
License: MIT
Last Refreshed: 2024-04-22 15:00:47
Package Statistics
Total Downloads: 37,268
Monthly Downloads: 1,391
Daily Downloads: 40
Total Stars: 9
Total Watchers: 4
Total Forks: 2
Total Open Issues: 0

Cookie

GitHub release License Build Status Scrutinizer Total Downloads

Your friendly, furry cookie monster!

Get and set cookies in PHP with ease. Supports vanilla PHP and Laravel 5.

CAUTION! Never store sensitive data in a cookie!

Installation

Install this package through Composer:

composer require codezero/cookie

Vanilla PHP Implementation

Autoload the vendor classes:

require_once 'vendor/autoload.php'; // Path may vary

And then use the VanillaCookie implementation:

$cookie = new \CodeZero\Cookie\VanillaCookie();

If you want your cookies to be encrypted, pass an instance of codezero/encrypter to the Cookie class. You will also need to provide it with an encryption key that is needed to decrypt the cookie later on.

$key = 'my secret app key';
$encrypter = new \CodeZero\Encrypter\DefaultEncrypter($key);
$cookie = new \CodeZero\Cookie\VanillaCookie($encrypter);

TIP: Laravel automagically encrypts cookies by default!

Laravel 5 Implementation

Add a reference to LaravelCookieServiceProvider to the providers array in config/app.php:

'providers' => [
    'CodeZero\Cookie\LaravelCookieServiceProvider'
]

Then you can "make" (or inject) a Cookie instance anywhere in your app:

$cookie = \App::make('CodeZero\Cookie\Cookie');

TIP: Laravel's IoC container will automatically provide the Laravel specific Cookie implementation. This will use Laravel's Cookie goodness behind the scenes!

Usage

Get a cookie

This will return null if the cookie doesn't exist or is expired.

$cookieValue = $cookie->get('cookieName');

Store a cookie for a limited time

If you don't specify $minutesValid, a default of 60 minutes will be used.

$minutesValid = 120;
$cookie->store('cookieName', 'cookieValue', $minutesValid);

Store a cookie forever

5 years feels like forever... ;)

$cookie->forever('cookieName', 'cookieValue');

Delete a cookie

If the cookie doesn't exist, nothing will happen...

$cookie->delete('cookieName');

Testing

$ vendor/bin/phpspec run

Security

If you discover any security related issues, please e-mail me instead of using the issue tracker.

License

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


Analytics