young-programa / secure-session-library
A secure, Composer-compatible session management library for PHP web applications. Developed by Raji Hamidu (BHU/24/MCSDF/CMP/009) for MCSDF project.
Package info
github.com/rajihamidy/secure-session-library
pkg:composer/young-programa/secure-session-library
Requires
- php: >=8.0
Requires (Dev)
- phpunit/phpunit: ^9.6
This package is not auto-updated.
Last update: 2026-03-15 22:44:28 UTC
README
🚀 Zero Configuration Setup
The library automatically handles everything:
- ✅ Creates database in writable location
- ✅ Sets up tables and indexes
- ✅ Manages session lifecycle
- ✅ No manual directory creation needed!
Quick Start
1. Install via Composer
composer require your-vendor/secure-session-library
2. Copy the Example
cp vendor/your-vendor/secure-session-library/examples/index.php ./
3. Run It
php -S localhost:8000
That's it! Visit http://localhost:8000/index.php
Minimal Code Example
<?php require 'vendor/autoload.php'; use SecureSession\{SecurityConfig, Logger, SessionManager, AnomalyDetector}; use SecureSession\Storage\SqliteStorage; // Zero configuration - everything is automatic! $config = new SecurityConfig(); $storage = new SqliteStorage(); // Auto-creates DB $logger = new Logger($storage, 'your-secret-key'); $sm = new SessionManager($config, $logger, new AnomalyDetector()); $sm->start(); // Done!
Database Location
The library automatically chooses the best writable location:
- System temp directory (most compatible):
/tmp/secure-session-library/session_logs.sqlite - Current working directory:
./data/session_logs.sqlite - Library directory (development):
vendor/.../data/session_logs.sqlite
You can also specify a custom path:
$storage = new SqliteStorage('/var/www/myapp/logs/sessions.sqlite');
Demo Credentials
- Username: demo
- Password: password
What to Test
Auto-Logout Feature
- Login with the demo credentials
- Wait for the configured idle timeout (default: 300 seconds / 5 minutes)
- Refresh the page - you'll be automatically logged out
To test faster: Modify the timeout in index.php:
$config->idleTimeout = 30; // 30 seconds for testing
Session Regeneration
- After successful login, the session ID is automatically regenerated
- Check the "Session Information" box to see the new session ID
Forensic Logging
- All session actions are logged to SQLite database
- View recent logs at the bottom of the page
- Logged actions include: create, regenerate, destroy, timeout, anomaly
Anomaly Detection
- Try accessing from different browsers/IPs
- The system detects suspicious changes in session context
File Structure
examples/
├── index.php # Main demo page
├── data/ # Auto-created for SQLite logs
│ └── session_logs.sqlite
└── README.md # This file
Configuration Options
Edit index.php to customize:
$config->idleTimeout = 300; // Session timeout in seconds $config->absoluteTimeout = 86400; // Max session lifetime (optional) $config->secureCookie = true; // Require HTTPS (production) $config->httpOnly = true; // Prevent JavaScript access $config->sameSite = 'Lax'; // CSRF protection
Database Location
By default, logs are stored in:
- Development:
examples/data/session_logs.sqlite - Production: Configure to use your app's writable directory
Troubleshooting
"No logs being saved"
- Check that the
datafolder exists and is writable - Verify PHP has permission to create SQLite files
- Check error logs:
tail -f /var/log/apache2/error.log
"Session not expiring"
- Verify
idleTimeoutis set to a low value for testing - Check that
$sm->start()is called on every page - Clear browser cookies and try again
"Permission denied"
chmod 755 examples/data chmod 644 examples/data/session_logs.sqlite
Production Deployment
When deploying to production:
-
Change the HMAC secret:
$secret = getenv('SESSION_LOG_HMAC'); // Use environment variable
-
Enable secure cookies:
$config->secureCookie = true; // Requires HTTPS
-
Use a writable directory:
$dbPath = '/var/www/writable/session_logs.sqlite';
-
Set proper file permissions:
chmod 755 /var/www/writable chmod 644 /var/www/writable/session_logs.sqlite
Support
For issues or questions, please visit: https://github.com/rajihamidy/secure-session-library or rajihamidu90@gmail.com