I want to define a variable in php with in a global scope. That variable should be accessible for each user, so it should not depend on the session.
I want to do something like the following: I have a php script, and that script usually takes about 5 to 15 minutes to execute, but it can take more than one hour in some special cases. This script is being run using a cron job with a 15 minutes interval. This script can also be run manually by any user.
Now it's possible to run any number of scripts at the same time. I want to limit this number of scripts.
So, I want a variable to store the number of currently executing scripts. I don't want to use a DB or files to store a single value.
Thanks in advance.
Writing the variables to a specific memory address and reading and writing to/from that address with information such as a count might work.
An (old) article on IPC (inter process communication) might help you
http://zez.org/article/articleview/46/
Out of interest why is it you don't want to use files or a database?
Another solution (despite more than likely technically using files or a database) would be to force the script to use a specific session ID.
Note in the example above, the call to session_write_close() is important because the execution of the script will prevent the counter being incremented until it finishes otherwise. And then as it closes it, it needs reopening to decrement the counter.
Obviously because of the session reopening, any output from the script would need to be buffered (see http://www.php.net/ob_start) to prevent headers already sent errors
Without DB or files this isn't possible as far as I am aware of. Instead, if you have access to the server (root access) you could check with the
ps
command (if running a linux OS) if the script is already running and if so, you could see how much instances of it.there's no way to save this somewhere else for everyone without file nor DB
Your preconditions are pretty restrictive. Memory is not (or not normally) shared between scripts and we cannot use files or databases: that virtually leaves no common space available to share information, so you have to calculate the information every time. Your best option is probably to inspect the running processes, identify which ones belong to your script and count them. Your mention to cron suggests Unix, which makes things easier. The POSIX extension probably allows to control process execution to this degree and, as last resource, you can always run the ps command from PHP.