1

Does anyone know an easy way to have PHP use MySQL for session data rather than its current flat file?

I have looked into zebra but I don't think it's workable on my solution.

Currently I use session_start(); and $foo = $_SESSION['bar']; in my scripts.

I have many 1000s of .php files all using this method, so its not possible to change them all to use a different method.

Zebra looks like it sort of fixes this, but still requires modification of all .php files which call session_start();. This isn't really practical.

I'm hoping there is some kind of module or settings within PHP that just says, "Store sessions in this table in this database, not in flat files." Then no modification to .php files would be needed and I can scale correctly.

Is this possible?

Or do I need to just settle for the fact that I will not have to manually edit 1000's of files because of this lack of support in PHP?

I honestly would have thought that in today's cloudy internet PHP would have something like this almost built in as an option. But alas, My Google skills have failed me.

TRiG
  • 10,148
  • 7
  • 57
  • 107
Lynxus
  • 151
  • 1
  • 13
  • Why do you want this? – 1615903 Jan 13 '15 at 10:27
  • 1
    1000 php files, are you making the new google? –  Jan 13 '15 at 10:28
  • Why - So the frontend can be spanned across many webservers and have the backend on a separate mysql instance. This means session data would be available across all frontends. 1000+ php files.. Yes.. Its a VERY large and complex system. – Lynxus Jan 13 '15 at 10:31
  • 1
    If you have 1000's of PHP files doing the exact same thing, that is not called complex, some might call that a mess. You might want to refactor your code. Once you've done that you might want to look at [this answer](http://stackoverflow.com/a/2950504/3492835) or this page on [wikihow](http://www.wikihow.com/Create-a-Secure-Session-Managment-System-in-PHP-and-MySQL). But also take a look at Redis or Memcache for storing your sessions. You don't want your users to pound your database. – Sander Toonen Jan 13 '15 at 10:34

2 Answers2

1

If you want to change the way sessions are stored without changing all php files, you might be able to achieve that by playing with the php.ini session.save_handler (Even though I would rather do it by code using session_set_save_handler...). For scalability, I would recommend using Memcached rather than mysql though. Ie:

session.save_handler = memcached
session.save_path = "127.0.0.1:11211"

If you are sharing the sessions between servers obviously change the IP from local to something else.

Javier C. H.
  • 2,124
  • 2
  • 19
  • 30
  • Thanks, i think I will go with this.. From what I understand, I can have memcached running on all frontend servers and have them write sessions to all servers in a redundant manor.. I have a private lan between all frontends so this seems like a great option to replicate sessions across all servers. Thanks! – Lynxus Jan 13 '15 at 11:26
0

I am not sure why do you need this. This may prove to be a maintenance overhead at a later point.

You may want to look at session_set_save_handler

Edit: I found a good tutorial here

Community
  • 1
  • 1
kranthi117
  • 628
  • 1
  • 8
  • 21
  • Its so sessions can be read from multiple frontend servers.. Ie: if a user moves server, their session is still available.. From what I can see, Set save handler is a problem as it requires editing every php file I have that uses sessions. It seems odd that something like PHP doesnt have a way to edit the storage engine for sessions. Am I misunderstanding something maybe? – Lynxus Jan 13 '15 at 10:34
  • For that you'll have to use a Load Balancer. Do not try to re-invent the wheel – kranthi117 Jan 13 '15 at 10:35
  • I dont have access to a loadbalancer on the platform used. It needs round robin DNS unfortunately. This is why I have this problem ( and the question ) – Lynxus Jan 13 '15 at 10:37