8

How can I use session in database table with php and mysql?

shin
  • 1,675
  • 4
  • 22
  • 46
  • 1
    Can you give us a little more information? What exactly are you going to do with the sessions? – 2ndkauboy Jun 01 '10 at 14:02
  • 2
    You may want to search on Google first. One of the first link that it will provide you is this, for instance: http://www.devshed.com/c/a/PHP/Storing-PHP-Sessions-in-a-Database/ – nico Jun 01 '10 at 14:10
  • Usually session stored in temporary files in server. I want to store session in database table. – shin Jun 01 '10 at 14:22

4 Answers4

20

You would need to create an object like so:

class SessionHandler 
{ 
    private static $lifetime = 0; 

    private function __construct() //object constructor
    { 
       session_set_save_handler(
           array($this,'open'),
           array($this,'close'),
           array($this,'read'),
           array($this,'write'),
           array($this,'destroy'),
           array($this,'gc')
       );
    }

   public function start($session_name = null)
   {
       session_start($session_name); //Start it here
   }

    public static function open()
    {
        //Connect to mysql, if already connected, check the connection state here.

        return true;
    }

    public static function read($id)
    {
        //Get data from DB with id = $id;
    }

    public static function write($id, $data)
    {
        //insert data to DB, take note of serialize
    }

    public static function destroy($id)
    {
       //MySql delete sessions where ID = $id
    }

    public static function gc()
    {
        return true;
    }
    public static function close()
    {
        return true;
    }
    public function __destruct()
    {
        session_write_close();
    }
}

Then before session_start initiate this class!

include 'classes/sessionHandlerDB.php';

$session = new SessionHandler();

$session->start('userbase');

$_SESSION['name'] = 'Robert Pitt'; //This is sent to SessionHandler::write('my_id','Robert Pitt')
echo $_SESSION['name']; //This calls SessionHandler::read($id)//$id is Unique Identifier for that

http://php.net/manual/en/function.session-set-save-handler.php

http://php.net/manual/en/function.serialize.php

Kevin ABRIOUX
  • 16,507
  • 12
  • 93
  • 99
RobertPitt
  • 56,863
  • 21
  • 114
  • 161
  • Can you tell me how can I use this code to set session? Is there any difference with normal session setting? – shin Jun 01 '10 at 14:20
  • Bascially whaen you use $_SESSION... This will query the SessionHandler, meaning the session handler is the middle man, it stors data and returns data, If you apply your own handler you can store it in anyway way you want,DB,File etc Executing this will tell PHP That when you use the Session to query this class for the data. aslong as you include this file and create an instance of it everything else you do will not change accept the storage method will be DB. Ive updated my post, please review – RobertPitt Jun 01 '10 at 14:26
  • Also this code is purly example, i would extend it to have a storage object so you reduce the calls to db, also look at MemCached! – RobertPitt Jun 09 '10 at 14:49
  • A suggestion (possible since PHP 5.4): the functions required for [session_set_save_handler()](https://www.php.net/manual/en/function.session-set-save-handler.php) are also declared in an interface ( [SessionHandlerInterface](https://www.php.net/manual/en/class.sessionhandlerinterface.php) ) specifically designed for this : the class should implement it. It is then possible to register the SessionHandler object without refering to every function/method. – Yosko Jan 21 '23 at 20:17
1

You control this in php.ini under the session_handler directive. Check out http://www.tonymarston.net/php-mysql/session-handler.html for a easy walk through (used it before).

Matt S
  • 1,767
  • 8
  • 9
1

You'll need to usesession_set_save_handler to write custom open, close, read, write, destroy, and garbage collection functions.

ceejayoz
  • 176,543
  • 40
  • 303
  • 368
0

See my github code snippet PHP5.4-DB-Session-Handler-Class for a database driven session management class in PHP 5.4.

kta
  • 19,412
  • 7
  • 65
  • 47