Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php share variable among different users/sessions

Tags:

php

i want to have a variable that has the same value for all the different users/clients accessing the system.

also the variable's value has to be modifiable.

like if any one user changes the value, the change has to get reflected to all other users too.

like image 736
nikunj Avatar asked Aug 13 '10 09:08

nikunj


People also ask

How can I pass variable from one page to another in PHP using session?

This will ensure that we can safely access the variable defined in other page, by just using $_SESSION['name']. In printName. php file, echoing the session name variable prints the name we have inputted from user in another page. So, this is how you pass variables and values from one page to another in PHP.

How to set the session variable in PHP?

To set session variables, you can use the global array variable called $_SESSION[]. The server can then access these global variables until it terminates the session. Now that you know what a session is in PHP and how to start one, it's time to look at an example and see how it works.

Can user edit session variables?

Session variables on the client are read-only. They cannot be modified.

Is PHP session per user?

The session is unique to whomever has that session ID, which is determined by the cookie. Yes, it's unique to each user. Variable scope means nothing here. Each request handled by PHP is isolated and has its own global scope (unless you have built your own daemon or something).


2 Answers

Store the variable in a shared space like

  • A file

  • A database record (easiest for implementing locking, see below)

  • A memcache bucket

you can easily modify it there. You may need to use some sort of locking mechanism to prevent race conditions when multiple users try to edit the value at the same time.

like image 51
Pekka Avatar answered Oct 02 '22 14:10

Pekka


Just use a database table for storing that value.

That's the simplest way to store persistent application-wide data in a Web application.

like image 39
Artefacto Avatar answered Oct 02 '22 14:10

Artefacto