0

I've found already Own SuperGlobal Variable in PHP? here it says that there is no way to somehow define/use a "server variable" that would be same for different users/sessions. But since few years has passed may be something has changed? And now such would be possible?

Basically I want to run a php script triggered by user event, but I don't want to run more often then e.g. once per 30mins. So my frst thought was keep a "server side var (that is visibles for all sessions/clients)" and if user trigger the event (e.g. open some page) check if script been running last 30 mins then just open the page, if no then before opening page e.g. purge outdated/passed things.

Yes I do understand I can do workaround with mySQL etc... but why to do workaroud if PHP would support what I need.

Thank you in advance!

2
  • Please see this meta post - How do I get attention for old, unanswered questions?. Simply posting the same question again is not the way things are done on Stack Overflow. Commented Mar 24, 2014 at 11:26
  • Apart from that, for the special use case you are describing I’d probably just go with a simple file in combination with filemtime and touch Commented Mar 24, 2014 at 11:31

1 Answer 1

1

A regular PHP variable cannot be persisted beyond the current execution of the script. Once a script ends, all its variables are gone. That's the basic nature of PHP; in fact it's the basic nature of all computer programs, it's just that PHP is typically always terminated very quickly. To persist data beyond a single script invocation, you have several options where to store that data:

  • a database
  • a file
  • shared memory (though this may be purged at any time)
  • a memory cache like memcached (basically an in-memory database server)
  • run a persistent PHP script and communicate with it via sockets (basically a memcache)
  • don't terminate your script, write it as a daemon

Nothing of this has changed recently.

Sign up to request clarification or add additional context in comments.

3 Comments

deceze, thank you for interesting information about computer programs. Since PHP server is running "all the time" my expectation it to have something that I'm looking for was reasonable (IMHO)
Nope, "PHP server" is not running. Apache (or nginx or some other web server) is running, which launches an independent PHP instance for each request. PHP scripts themselves are started up and torn down constantly. To write a persistent script, you explicitly have to make it persistent (which typically means an endless loop while (true) { ... }). This isn't really any different than any other program in any other language as well.
i have to agree with that :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.