- Notifications
You must be signed in to change notification settings - Fork7
👑 Session2DB - A drop-in replacement for PHP's default session handler which stores session data in a MySQL database, providing both better performance and better security and protection against session fixation and session hijacking.
License
voku/session2db
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
A drop-in replacement for PHP's default session handler which stores session data in a database, providing both better performance and better security and protection against session fixation and session hijacking.
Session2DB implementssession locking - a way to ensure that data is correctly handled in a scenario with multiple concurrent AJAX requests.
It is also a solution for applications that are scaled across multiple web servers (using a load balancer or a round-robin DNS) and where the user's session data needs to be available. Storing sessions in a database makes them available to all of the servers!
The library supports "flashdata" - session variable which will only be available for the next server request, and which will be automatically deleted afterwards. Typically used for informational or status messages (for example: "data has been successfully updated").
Session2DB is was inspired by John Herren's code from theTrick out your session handler article andChris Shiflett's articles about PHP sessions and based onZebra_Session.
The code is heavily commented and generates no warnings/errors/notices when PHP's error reporting level is set to E_ALL.
PHP 7.x with themysqli extension activated, MySQL 5.x+ (recommanded:mysqlnd extension)
composer require voku/session2db
After installing, you will need to initialise the database table from theinstall directory from this repo, it will containing a file namedsession_data.sql. This file contains the SQL code that will create a table that is used by the class to store session data. Import or execute the SQL code using your preferred MySQL manager (like phpMyAdmin or the fantastic Adminer) into a database of your choice.
*Note that this class assumes that there is an active connection to a MySQL database and it does not attempt to create one!
//// simple (dirty) example//<?phpusevoku\db\DB;usevoku\helper\Session2DB;DB::getInstance('hostname','username','password','database');newSession2DB();// from now on, use sessions as you would normally// this is why it is called a "drop-in replacement" :)$_SESSION['foo'] ='bar';
//// extended example//<?phpusevoku\db\DB;usevoku\helper\DbWrapper4Session;usevoku\helper\Session2DB;// include autoloaderrequire_once'composer/autoload.php';// initialize the database connection e.g. via "voku\db\DB"-class$db =DB::getInstance('hostname',// e.g. localhost'username',// e.g. user_1'password',// e.g. ******'database',// e.g. db_1'port',// e.g. 3306'charset',// e.g. utf8mb4true,// e.g. true|false (exit_on_error)true,// e.g. true|false (echo_on_error)'',// e.g. 'framework\Logger' (logger_class_name)''// e.g. 'DEBUG' (logger_level) );// you can also use you own database implementation via the "Db4Session"-interface,// take a look at the "DbWrapper4Session"-class for a example$db_wrapper =newDbWrapper4Session($db);// initialize "Session to DB"newSession2DB('add_your_own_security_code_here',// security_code0,// session_lifetimefalse,// lock_to_user_agentfalse,// lock_to_ip1,// gc_probability1000,// gc_divisor'session_data',// table_name60,// lock_timeout$db_wrapper,// db (must implement the "Db4Session"-interface)true// start_session (start the session-handling automatically, otherwise you need to use session2db->start() afterwards) );// from now on, use sessions as you would normally// this is why it is called a "drop-in replacement" :)$_SESSION['foo'] ='bar';// data is in the database!
For support and donations please visitGithub |Issues |PayPal |Patreon.
For status updates and release announcements please visitReleases |Twitter |Patreon.
For professional support please contactme.
- Thanks toGitHub (Microsoft) for hosting the code and a good infrastructure including Issues-Managment, etc.
- Thanks toIntelliJ as they make the best IDEs for PHP and they gave me an open source license for PhpStorm!
- Thanks toTravis CI for being the most awesome, easiest continous integration tool out there!
- Thanks toStyleCI for the simple but powerfull code style check.
- Thanks toPHPStan &&Psalm for relly great Static analysis tools and for discover bugs in the code!
About
👑 Session2DB - A drop-in replacement for PHP's default session handler which stores session data in a MySQL database, providing both better performance and better security and protection against session fixation and session hijacking.
Topics
Resources
License
Stars
Watchers
Forks
Packages0
Languages
- PHP100.0%