Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Same variable for everyone php?

I was wondering, is there a way to have a sort of variable that would be 'static' meaning that it would be the same for all users in php ? I know that there is a possibility to have a static variable within a function in php but that is not what I want.

I would like everyone to share an object which I would manipulate depending on the user's demand. Or, another example that is similar to what I want is, is there a way to keep a variable that counts the number of visitors (without using any sort of file or database manipulation). That variable would be incremented every time a user come to my page.

Cheers !

like image 528
David Avatar asked Mar 03 '11 20:03

David


2 Answers

Without using a file or database, I believe you could do this using something like APC.

$var = 1;
$key = 'myVariable';
apc_store($key, $var);
echo apc_fetch($key); // 1

If you want to increment it, you can use apc_inc()

echo apc_inc($key); // 2

However, this variable won't be preserved if the cache is cleared (which happens when it fills up or the server is restarted).

like image 110
Joe Lencioni Avatar answered Oct 04 '22 12:10

Joe Lencioni


Check out semaphores and shared memory and how they work in PHP. With a shared memory variable different processes (users) can use the same memory space and use the same variables. Here's a link to the PHP documentation to get you started:

http://www.php.net/manual/en/function.shm-get-var.php

like image 24
user470714 Avatar answered Oct 04 '22 13:10

user470714