I want to define a variable with superglobal scope

2019-06-14 07:06发布

问题:

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.

回答1:

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.

<?php

$maximum = 1;

session_id(md5('myscript'));
session_start();

if( !isset( $_SESSION['count'] ) ) {
    $_SESSION['count'] = 1;
} else {
    if( $_SESSION['count'] >= $maximum ) {
        die( "too many processes running" );
    } else {
        $_SESSION['count']++;
    }
}

session_write_close();

// simulate running something
sleep(10);

session_id(md5('myscript'));
session_start();

$_SESSION['count']--;

session_write_close();

echo "<br />executed";

?>

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



回答2:

  1. check if limit is reached
  2. when user access script, add him to DB
  3. when user exists (or script ends), remove him from DB

there's no way to save this somewhere else for everyone without file nor DB



回答3:

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.



回答4:

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.