19

How can I keep variables in the server's RAM between script executions, and even between different sessions?

Do I need to install some sort of extension, or is this built into PHP somehow?

Blackbam
  • 17,496
  • 26
  • 97
  • 150
JJJollyjim
  • 5,837
  • 19
  • 56
  • 78

2 Answers2

11

There is few options:

Memcache http://memcached.org/ extension. It's RAM based storage engine.

APC APC - PHP manual apc code cache allows store variables.

If you don't want any extensions you could store your data into file (serialize, or xml format), and it will be persistent data. Slower then memory storage.

And if you want to store general data, well then there is "one-hundred-two" database engines. For example MySQL, SQLite or NOSQL MongoDB and more...

Aurimas Ličkus
  • 9,886
  • 4
  • 24
  • 26
  • 1
    Would a DB not be as slow as a filesystem? I have used files in the past, but was annoyed by the lack of speed – JJJollyjim Jun 03 '12 at 08:01
  • DB will be faster, because db'a internally optimised for efficiency, uses caching, fast algoritms for data search. And it's more reliable way of storing data. – Aurimas Ličkus Jun 03 '12 at 08:05
  • 2
    You should be using APC if you have a single server, and Memcached - if you work with cluster. That's because Memcached is a bit slower then APC, but **distributed**. – tereško Jun 03 '12 at 12:36
0

You can cache a variable in the data store using apc_store() and apc_add(), then use apc_fetch() to get their value. But I think session variables $_SESSION would be a good option if your variables are related to only one client. If the variables are GLOBAL & are used between many clients, then using database engines would also be an option.

Zulkifil
  • 337
  • 4
  • 4
  • 1
    Welcome to Stack Overflow. APC is referenced in the [other answer](https://stackoverflow.com/a/10868702/3536236). `$_SESSION` does not apply (or if it does apply you need to elaborate as to how) as the question states "`and even between different sessions`". – Martin May 17 '19 at 16:57