GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - kodedphp/session: A session library with custom handlers and php.ini support.

BranchesTags
Open more actions menu

Latest commit

 

History

89 Commits
89 Commits

Folders and files

NameName
Last commit message
Last commit date
Handler
Handler
 
 
Tests
Tests
 
 
.gitattributes
.gitattributes
 
 
.gitignore
.gitignore
 
 
.scrutinizer.yml
.scrutinizer.yml
 
 
.travis.yml
.travis.yml
 
 
LICENSE
LICENSE
 
 
README.md
README.md
 
 
Session.php
Session.php
 
 
SessionAuthenticatedMiddleware.php
SessionAuthenticatedMiddleware.php
 
 
SessionConfiguration.php
SessionConfiguration.php
 
 
SessionMiddleware.php
SessionMiddleware.php
 
 
VERSION
VERSION
 
 
composer.json
composer.json
 
 
functions.php
functions.php
 
 
phpunit.xml
phpunit.xml
 
 

Repository files navigation

Koded Session

Latest Stable Version Build Status Code Coverage Scrutinizer Code Quality Minimum PHP Version Software license

The library relies on the php.ini settings. Every session ini directive can be reset with the Koded\Session\SessionConfiguration object.

Refer to php.ini session directives: http://php.net/manual/en/session.security.ini.php

Usage

The session is started automatically by using one of the 2 methods:

app configuration

[
    'session' => [
        // your ini "session." overwrites, without "session." prefix
    ]
]

or using a SessionMiddleware

include this middleware class in your middleware stack

// your middleware stack
$middleware = [
    SessionMiddleware::class
];

Session class and function

session()->get('key');
session()->set('key', 'value');
// etc.

The session class can be instantiated and used, but the function session() is recommended instead an instance of Session class.

Handlers Configuration

The bare minimum is to define the handler you want to use for the session:

// in your configuration file

return [
    'session' => [
        'save_handler' => 'redis | memcache'
    ]
]

If you do not choose one of the Redis or Memcached, it defaults to files handler which is the PHP's default session mechanism.

However, the files handler might not be desirable if your application runs in Docker, Kubernetes, distributed environment, etc.

The best choice for PHP sessions is Redis in almost all situations.

WARNING: Memcached may drop the session data, because it's nature. Use it with caution!

Redis handler

[
    'session' => [
        'save_handler' => 'redis'
        
        // OPTIONAL, these are the defaults
        'host' => 'localhost',
        'port' => 6379,
        'timeout' => 0.0,
        'retry' => 0,
        'db' => 0,
        
        'prefix' => 'sess:',
        'serializer' => 'php', // or "json"
        'binary' => false,     // TRUE for igbinary
    ]
]

A typical Redis settings:

  • 1 server
  • application + redis on the same machine
  • Redis (127.0.0.1:6379)
  • no auth (Redis is not available from outside)
[
    'session' => [
        'save_handler' => 'redis',
        'name'         => 'session-name',
        'prefix'       => 'sess:',

        // isolate the session data in other db
        'db'           => 1
    ]
]

To support huge volumes you need a good sysadmin skills and wast knowledge to set the Redis server(s).

Memcached handler

[
    'session' => [
        'save_handler' => 'memcached',
        
        // OPTIONAL: defaults to ['127.0.0.1', 11211]
        // If you have multiple memcached servers
        'servers' => [
            ['127.0.0.1', 11211],
            ['127.0.0.1', 11212],
            ['127.0.0.2']
            ...
        ],
        
        // OPTIONAL: the options are not mandatory
        'options' => [
            ...
        ]
    ]
]

A typical Memcached settings:

  • 1 server
  • application + memcached on the same machine
  • Memcached (127.0.0.1:11211)
[
    'session' => [
        'save_handler' => 'memcached',
        'name'         => 'session-name',
        'prefix'       => 'sess.'
    ]
]

To support huge amount of users you need a decent amounts of RAM on your servers. But Memcached is a master technology for this, so you should be fine.

Files handler

This one is not recommended for any serious business. It's fine only for small projects.

All session directives are copied from php.ini.

[
    'session' => [
        // OPTIONAL: defaults to "session_save_path()"
        // the path where to store the session data
        'save_path' => '/var/www/sessions',
        'serialize_handler' => 'php'
    ]
]

A typical native PHP session settings:

[
    'session' => [
        // really nothing,
        // skip this section in your configuration
    ]
]

You cannot use this handler if you've scaled your application, because the session data will most likely be handled randomly on a different instance for every HTTP request.

About

A session library with custom handlers and php.ini support.

Topics

memcachedmiddlewarephp-sessionsredissessionsession-handler

Resources

Readme
BSD-3-Clause license
Activity
Custom properties

Stars

3 stars

Watchers

2 watching

Forks

0 forks
Report repository

Releases

Packages

Used by

Contributors

Languages

You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.