New

Just launched: Vueblocks, a brand-new search engine for Vue packages! Explore Vueblocks →

codezero/cookie

Your friendly, furry cookie monster. Get and set cookies with ease.
73,923 10
Install
composer require codezero/cookie
Latest Version:2.0.0
PHP:>=5.4.0
Laravel:5 · 6
License:MIT
Last Updated:Sep 27, 2019
Links: GitHub  ·  Packagist
Maintainer: codezero

PHP Cookies

GitHub release License Build Status Code Coverage Scrutinizer Code Quality Total Downloads

ko-fi

Get and set cookies in vanilla PHP with ease. A Laravel implementation is included, but this has no real advantages if you only use Laravel.

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

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

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

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

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

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

5 years feels like forever... ;)

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

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

$cookie->delete('cookieName');

You can check if a cookie exists. However, keep in mind that a cookie will not be available immediately. It will be on the next page load.

if ($cookie->exists('cookieName')) {
    // The cookie exists!
}

Testing

$ composer run test

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.

Related Packages

codezero/encrypter

Encrypt and decrypt strings in PHP.

100,316 2
marcogermani87/filament-cookie-consent

Easy cookie consent integrations for Filament

18,027 19
valorin/random

Random is a simple helper package designed to make it easy to generate a range o...

413,065 233
php
cline/variable-keys

Laravel Blueprint macros for variable primary key and morph types

22,912 0
php
cline/struct

Lean, attribute-driven Laravel data objects with explicit validation and Eloquen...

17,788 0
php

Version History

Version Released PHP License
2.0.0 >=5.4.0 MIT
1.0.2 >=5.4.0 MIT
1.0.1 >=5.4.0 MIT
1.0.0 >=5.4.0 MIT