tnapf/mysqlsessions

This package is abandoned and no longer maintained. No replacement package was suggested.

A sessioninterface implementation for storing sessions in a mysql database

v0.1.0 2023-02-13 01:16 UTC

This package is auto-updated.

Last update: 2024-05-01 00:06:10 UTC


README

A sessioninterfaces implementation for using a MySQL database as session storage

Installation

composer require tnapf/mysqlsessions

Usage

Setting up table

First, use this SQL code to create your sessions table

SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
START TRANSACTION;

CREATE TABLE `sessions` (
  `id` varchar(16) NOT NULL,
  `data` longtext CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL CHECK (json_valid(`data`)),
  `expires` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

ALTER TABLE `sessions`
  ADD PRIMARY KEY (`id`);
COMMIT;

Creating Session Controller

Next create a PDO Connection (will be using tnapf/pdo for building the driver)

use Tnapf\Pdo\Driver;
use Tnapf\MysqlSessions\Controller;

$driver = Driver::createMySqlDriver("root", "password", "database")->connect();

/** @var PDO $driver */
$driver = $driver->driver;

$sessions = new Controller($driver);

After creating the driver construct Tnapf\MysqlSessions\Controller using the PDO object as the first argument.

Creating a session

$session = $session->create(); // you can supply a timestamp in seconds for when the cookie should expire; default is 7 days

header($session->setCookieHeader()); // sends a set-cookie header with the session id

Setting session variables

$session->var = "foo";

// or

$session->set("var", "foo");

Unsetting session variables

unset($session->var);

// or

$session->unset("var");

Deleting sessions

$sessions->delete($session);

// or

$sessions->delete($session->id);