0

I've been looking into making my own session handler for my PHP application, simply because I want to store the session data in a MySQL Memory table instead of on the local filesystem.

There are 4 main reasons for me to do this.

  1. The MySQL server is local, so reading from memory will be faster.
  2. Being able to list active users
  3. Making an easy way for an admin to read and modify a user session, without having them logging ind and out.
  4. Being able to run scripts after a session timeout, or a user "idle" variable in the session.

I can't seem to find any easy way for doing this.

I could use memcached, but it would result in doubling the session data in the memory on my server.

Does anybody know of an easy way, to force PHP at run-time, to use a MySQLi resource as session storage?

4
  • Look into PHP's function: session_set_save_handler: php.net/manual/en/function.session-set-save-handler.php Commented Dec 24, 2012 at 13:50
  • Have a look at this answer: stackoverflow.com/questions/5057466/… Commented Dec 24, 2012 at 13:50
  • The problem with settings the save handler, as people do in the PHP documentation, is that you have to use new functions for your sessions. This means I would have to change alot of code. I'm thing there might be a module, extension or something out there, which replaces/adds functionality to the original session handler. Commented Dec 24, 2012 at 13:54
  • @Drejer: No you won't. Just use the session save-handler that comes with the memcache extension and you're done. It's a simple setting. Commented Dec 24, 2012 at 13:58

1 Answer 1

1

Actually Mysql offers a memcache interface that first uses memory and can later on persitent into a real database table.

It can be simply configured via php.ini after you've create the tabels in your database server (see here):

; when using the "memcache" extension:
session.save_handler=memcache
; when using the "memcached" extension:
; session.save_handler=memcached

session.save_path="tcp://localhost:11211"

You find it outlined here with PHP examples:

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.