GithubHelp home page GithubHelp logo

stevencorona / sessionhandlercookie Goto Github PK

View Code? Open in Web Editor NEW
43.0 1.0 14.0 39 KB

A simple HMAC-cookie based session handler implementation for PHP 5.4's SessionHandlerInterface

License: MIT License

PHP 100.00%

sessionhandlercookie's Introduction

Cookie Session Handler for PHP

This library adds HMAC-Based Cookie sessions to PHP 5.4+

Cookie Session Handler is a short, but useful piece of code that I've decided to open source from my book, Scaling PHP Applications.

Sessions are a major source of scaling pains in PHP. By default, session data is stored on the filesystem in PHP, which doesn't scale horizontally as you add more servers (without sticky sessions or NFS). Typically, the way we solve this is by moving sessions to the database or memcached/redis. This punts the problem, but can cause high database load.

Session Data in the Cookie

What if we could store the session data in the cookie? It'd -easily- solve the scaling problem, but you'd have to worry about data tampering— remember, cookie data is not sercure and can be modified by the user.

We solve the data integrity problem the same way as many other popular frameworks (i.e, Rails) by storing the cookie data with an HMAC token.

How does it work?

PHP 5.4 adds the SessionHandlerInterface which allows for custom PHP session handlers.

It's easy to use and plug-and-play and it works transparently with the native session interface, through the $_SESSION global variable.

HMAC

This library uses PHP's Hash Extension (bundled with PHP as of 5.1.2). By default, it uses sha512, but you can change it to any hashing alogrithm supported.

To make this all work, you need to provide a secret that's used for the HMAC. By default, a very weak and predictable secret is used, and you should change it to your own secret.

Example Usage

<?php

$secret = "deadc0de";

$handler = new SessionHandler\Cookie($secret);
session_set_save_handler($handler, true);
session_start();

$_SESSION["foo"] = "bar";

Symfony Usage

framework:
    session:
        handler_id:  session.handler.cookie

services:
    session.handler.cookie:
        class:     SessionHandler\Cookie
        public:    true
        arguments:    ['secret']

License

The MIT License (MIT)

Copyright (c) 2014 Steve Corona Inc.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

sessionhandlercookie's People

Contributors

stevencorona avatar ivanlitovchenko avatar sed10 avatar kukoman avatar

Stargazers

S. Tougeiro avatar Harald Lapp avatar Gopal Sharma avatar  avatar Playinteractive avatar Public profile avatar Carson Evans avatar Sergey Shuchkin avatar wuxu avatar Libby Rodriguez avatar  avatar Alexandr MaximenkO avatar  avatar Kernel Jackson avatar X5 Engine avatar Brent Shaffer avatar Constantin Rack avatar BYUNGI avatar José Gaspar avatar  avatar Larry avatar Sean Huber avatar Tristan Havelick avatar Ray Silva avatar  avatar  avatar Daniel Doezema avatar Si Westcott avatar Mark Holtz avatar Joshua Coffey avatar John de Plume avatar Seongkyeong Lee avatar Derrick avatar  avatar Abhishek Das avatar Marcos Sader avatar kiasaki avatar Vijay avatar Austin Butler avatar  avatar Jamie York avatar Joshua Stauter avatar  avatar

Watchers

 avatar

sessionhandlercookie's Issues

Publish to Packagist.org

Could you publish this so it's a simple "composer install" to get it? The composer.json file is already there, should just be a case of clicking submit on packagist.org

Maximum cookie size easy to reach

Cookie size per domain is limited in some browsers and/or by web servers (when limited by a web server, the user would be forced to manually delete cookies before any more requests).

Before base64_encode, the data could be gzipped.
It will reduce size to at least half, for non-binary data (arrays of text strings, ints, etc.).
And it will provide some level of obsfucation.

Note: If you are ever considering adding encryption, encrypt after gzipping.

Warning message after implementation

After adding SessionHandlerCookie to the code my logs got flooded with these messages

NOTICE: PHP message: PHP Warning:  session_write_close(): Session callback expects true/false return value in Unknown on line 0
NOTICE: PHP message: PHP Warning:  session_write_close(): Failed to write session data using user defined save handler. (session.save_
path: /tmp) in Unknown on line 0

I'm running php 7.3.33

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.