PHP Session-like storage global across all users
Asked Answered
D

2

6

What is a good method to retain a small piece of data across multiple calls of a PHP script, in such a way that any call of the script can access and modify its value - no matter who calls the script?

I mean something similar to $_SESSION variable, except session is strictly per-client; one client can't access another client's session. This one would be the same, no matter who accesses it.

Losing the value or having it corrupted (e.g. through race conditions of two scripts launched at once) is not a big problem - if it reads correctly 90% of the time it's satisfactory. OTOH, while I know I could just use a simple file on disk, I'd still prefer a RAM-based solution, not just for speed, but this running from not very wear-proof flash, endless writes would be bad.

Dak answered 21/2, 2014 at 23:58 Comment(3)
what's wrong with storing these values in a database?Hipparch
You could use something like Memcached.Steading
@SamuelCook: Rolling out whole SQL layer just to hold a 30-character string (about as much as I need to store)? Plus the database is on disk.Dak
O
2

Take a look at shared memory functions. There are two libraries that can be used to access shared memory:

For storing binary data or one huge String, the Shared Memory library is better, whereas the Semahpores library provides convenient functions to store multiple variables of different types (at the cost of some overhead, that can be quite significant especially for a lot of small-sized (boolean for example) variables.

If that is too complex, and/or you don't worry about performance, you could just store the data in files (after all, PHPs internal session management uses files, too....)

Orgiastic answered 22/2, 2014 at 0:4 Comment(2)
It seems like Semaphores would solve my problem. I really don't need anything more heavyweight for like, 30 bytes of text?Dak
Oh, and if it's only one string, I would prefer shmop_* functions (part of the Shared Memory library) over the shm_* functions that Semaphores provides.Orgiastic
D
1

A good alternative to using a database would be memcache!

Differentia answered 22/2, 2014 at 0:4 Comment(3)
According to this: we-love-php.blogspot.de/2013/02/… Memcached is quite slow (slower than a DBMS)Orgiastic
Way too heavyweight for my liking. We're talking storing a single, small variable for goodness sake! Rolling out a separate daemon for that?Dak
Then you should just use plain temporary files. PHP session handling doesn't do anything else.Differentia

© 2022 - 2024 — McMap. All rights reserved.