Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the alternative to PHP session variables?

I am writing a brand new website and I'd like to make sure that it scales out easily if I ever get to the point where I must host the site on multiple machines with a load balancer.

The user of the website can be authenticated. In other words, I need to maintain some state information. My first reflex was to use Session variables but then I am going to be limited to a single machine. I know that there are ways to store the session variables outside (in a DB, redis, memcached) but is that the only options?

What are the alternative to session variable? How Facebook and other big web sites are doing this?

P.S. I am not looking for another session handler (DB, redis, etc.). I'd like to know if there a way to completely get rid of session variables.

like image 385
Martin Avatar asked Feb 07 '12 14:02

Martin


2 Answers

Ever heard of session_set_save_handler? It allows you to use mechanisms other than the default PHP session handler (the one that writes sess_xxxxxxxxxxxx files in tmp directory).

You can write your own session handler that uses a database. This could be a time consuming task; so you can stick with the default PHP session handlers for the time being and transparently switch to database when you are ready. You probably won't have to rewrite any code except implementing and plugging in your version of the six session handling functions.

like image 71
Salman A Avatar answered Nov 14 '22 23:11

Salman A


You can look into caching, i.e using Zend cache or APC cache, for example.

like image 36
JRL Avatar answered Nov 14 '22 22:11

JRL