tags:

views:

50

answers:

3

hi,

Is there a way, in php, to save a variable on server so it stay there for all of the requests (not session, need to be saved for all users ever)? i'm new in php, in asp.net is use the 'application' context.

thanks.

A: 

Yes, it's called session.

Marwelln
sorry, i ment diffrent users requests...
Nir
A: 

Hello,

look at $_SESSION variable ;-)

Arnaud F.
+2  A: 

Hello,

PHP does not have an application context, probably because of it's script nature. You may store data for an user and for a limited period if time in the session, but if you want something per application you should implement it yourself. Storing in a database would be best. By using a class that implements ArrayAccess you will replicate an application context.

If you want some sort of fast access to you data you could use Memcache. But that is not really application data. It's just caching. That means you have to have a fallback to something (database) if the data you want is no longer found in the cache.

So if you want a fast and reliable way to store application data in PHP you should do it with a database table for storing it and Memcache for fast access.

Alin Purcaru
going to the database will be terrible... there is no way to implement it on the server memory?
Nir
Why do you say that? You need to store (application) data. That's what databases are for. I would have suggested file system too, but I think that you should store in a table in the database because it's safer.
Alin Purcaru
See my edit for storing in server memory.
Alin Purcaru
lets say i have a very complex query that takes time to execute. the query result would not change in the near hour for sure. how can i avoid keep executing the query every minute?
Nir
'memcache' looks great. i'll check it! thanks!!
Nir
As I said in my answer, you still need the permanent storage location. Memcache may last a very long time, but it's still a cache and does not offer any guarantee that you will find you data in it.
Alin Purcaru
@Nir make this query run fast.
Col. Shrapnel
Even a very fast query usually won't match taking the result from memory. i wont trust 'memcache' blindly if it return nothing then i'm going to the db... but if it's there i'm good.
Nir
Seems you're all sorted out. Have fun coding it!
Alin Purcaru
@Nir millions sites does keep their data in the database and quite happy with it. I doubt your site is something special.
Col. Shrapnel
You misunderstood me. i don't say the database is a waste - i say that i want to avoid visiting him unnecessarily.
Nir