Forum Moderators: coopster

Message Too Old, No Replies

Persistant Memory

How can I share data across php sessions?

         

eaden

9:54 am on Mar 29, 2003 (gmt 0)

10+ Year Member



Hi there,
Basicly, I have some php scripts that need to map an id to a name. That mapping is in a database, but querying every time I need to map an id to a name is too many db accesses.

What I would like is some sort of shared memory array, which is available across php sessions. So in my code I have :

if(isset($my_shared_array[$id])) {
$name = $my_shared_array[$id];
} else {
$my_shared_array[$id] = $db->get_var("select name from idnamemap where id=$id");
$name = $my_shared_array[$id];
}

$my_shared_array is available to all php apps, and doesn't expire when the page finishes running, but is available for the next one that runs as well.

The question is how to do this persistancy stuff? I have looked at the shared memory in php, but I'm not sure if it's what I'm looking for, or how to apply it to my situation.

In a way, it is a kind of db result cache, so maybe something else would work as well.

I'm looking for the method that is the quickest / lowest demanding of system resources.

DrDoc

4:52 pm on Mar 29, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Have you looked at PHP Session Management [php.net]? It provides exactly what you're asking about. If you don't have PHP4, you can always use cookies [php.net].

eaden

12:31 am on Mar 30, 2003 (gmt 0)

10+ Year Member



Well session mamagement works within a session, but not across sessions as I need. For example, I need to share info with mary joe and bob.

I want an array to be stored in memory, that is accable to all users. PHP sessions are a per user thing.

andreasfriedrich

2:53 am on Mar 30, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



As long as you are on a *nix server what´s wrong with shm_attach [php.net], shm_put_var [php.net], et al.? If you want these data stored on disk you could use a shared session as well. Just have one session_id [php.net] like 1 that you share among all users.

Andreas

eaden

3:07 am on Mar 30, 2003 (gmt 0)

10+ Year Member



Thanks andreasfriedrich that looks like what I want, although I don't know how to apply it to my situation. I.e. you can specify the size of the memory, but it's an array that will change size with time. And using a session with only 1 session ID does work, but I need to use sessions for logins and I don't think I han have 2 sessions running at once. Also using the one session id it still sets cookies etc.

Idealy thered be a "server_session" equilivent of the session functions that can be shared. But thanks, I'll see how I go with shm.