1

This is a pretty long question in words, but it all adds up to one function.

For some time, I have tried figuring out how to increment a variable, save it and then display it. Only problem is I figured out that to save the variable and allow all users on the website to see the same updated variable, it has to be saved server side, so now I have a big question as I have tried to figure out one thing, and now has to do it another way.

To store a variable server side, is it enough to save the variable in a .php script because if so, I think what I am looking for is a way to have a variable in a .php script, then when a specific function is called I want to increment this variable by one, and then last I want to save this variable server side, to always be able to have the exact same variable on all users screens. The .php script is called by a form from the html script, so there is no problem there, the problem is as mentioned above, how to make a variable in .php script, increment that variable when a function is called, save that variable and then pass it back to the html page to display.

Sm00rf9000
  • 531
  • 1
  • 7
  • 20
  • If 100 people are logged on and looking at the variable and one user triggers an update/increment, should all users immediately see the new value? – BigScar Mar 31 '15 at 23:56
  • Yes if they reload the website, it should be the updated value. – Sm00rf9000 Apr 01 '15 at 00:13
  • If it only needs to be updated on reload, then Luis' answer below is the right direction to go. – BigScar Apr 01 '15 at 00:16

1 Answers1

0

At first one may think of a Session variable, But as you pointed out it should be shared among all users, you need to save it into a database, file or use a cache server.

Are you using a database? You can create a single table containing a key and value columns. Then you keep the row with the specified key up-to-date with the variable value you want to keep track of.

If you don't have access to a database server, you can serialize the variable or simply store it as a text file and read from it. Only problem here is concurrency, if you have too many users at the same time, you won't be able to update it at once.

Cache server is a bit more complicated to explain, but you could look for further documentation about that. Good luck!

LuisTensai
  • 23
  • 6
  • Thank you for the answer, by any chance do you have som links or alike that can point me in the direction? – Sm00rf9000 Apr 01 '15 at 00:28
  • Sorry for the late answer: http://stackoverflow.com/questions/2995461/save-php-variables-to-a-text-file Maybe you can use SQLite 3 within your server. Check http://www.tutorialspoint.com/sqlite/sqlite_php.htm Good luck! – LuisTensai Apr 01 '15 at 17:36