There is a PHP script on the server which runs about 20 - 30 seconds depending on the size of the file it's working on.
To stop the running instance when a new one ist started I change a session variable. Within the work loop in the script I am checking if this session variable has changed to stop the execution. And this does not work out. Why is this ?
Basically I am starting the session first:
session_start();
Then I generate a random number, assing this to an instance variable, kill the belonging session variable and assign the generated number to that session variable:
$this->number = rand();
unset( $_SESSION[ 'number' ] );
$_SESSION[ 'number' ] = $this->number;
Within the loop I let this $_SESSION[ 'number' ] being checked for a change which should appear when a new script instance is started:
for( $i = 0 to 1.000.000 ){
$s = $_SESSION[ 'number' ];
if( $this->number !== $s ){
die();
}
So let's say:
script1 starts the session, stores 1 in $_SESSION[ 'number' ] and checks changes to $_SESSION[ 'number' ] to die while looping.
script2 starts and stores 2 in $_SESSION[ 'number' ]
At that moment script1 should get aware of this change and stop working
what is does NOT.
Please be so kind and tell me why this does not work out, as I let the script echo the actual $_SESSION[ 'number' ] on start and see that the see the number generated from the script started before, being changed the by the last started instance.
Changes you are making to the global
$_SESSIONwill not propagate across different scripts which are already running.... From the definition:The session is unique to the current running script and its basically an array which is populated when the code is loaded - Even if you are using the same session for multiple scripts they will have a copy of the current values when called. Changes to the stored session values will not reflect unless you re-run the script.
You need to implement another mechanism to "signal" across scripts.
The best ways are (my opinion):