happyhardik
happyhardik

Reputation: 25527

PHP - what is an alternative to session variables

I am storing a lot of variables in session, which is creating a performance problem. So, I have been asked to store it somewhere else, I can store it in the database, but that would again be slow.

Is there any better alternative to store session variables? Global variable are per file/request. While cookies will open the variables to users and will not keep it server side.

Thanks in advance for your answers!

Upvotes: 4

Views: 6069

Answers (3)

Homan
Homan

Reputation: 26718

It depends on how many is "a lot" of variables? The default session storage for rails is using cookies, which is usually enough for me. The cookie is encrypted if you're worried about exposing the variables. Do you have to have it server side? With html5 you have localStorage options.

Upvotes: 0

bumperbox
bumperbox

Reputation: 10214

php sessions can be configured to work in many ways. you can run it from memcache if you have enough free memory on the server. that would be high performance

you can also use database to store sessions information but as you say this can be slow.

why is it currently creating performance problems? is there a large number of sessions being created or do you store large amounts of data in the session?

Upvotes: 2

Amber
Amber

Reputation: 526553

Consider memcached for semi-persistent data like this. Store the cache key in $_SESSION and then use it to grab your cached data.

Since memcached caches everything in memory (and is strictly a key-value store), it's faster than a database. It's somewhat ideal for things like sessions, because if you happen to lose the cached data, nothing serious is lost (the user just gets unexpectedly logged out).

In fact, the PHP Memcache implementation provides a session handler (see Example #2) which could transparently handle your sessions for you without you really needing to make any modifications to your code.

Upvotes: 5

Related Questions