Does session variables work when using background process?
I have two php scripts - index.php:
session_start();
$_SESSION['test'] = 'test';
$WshShell = new COM("WScript.Shell");
$oExec = $WshShell->Run("C:/xampp/php/php-cgi.exe -f C:/xampp/htdocs/sand_box/background.php".session_id(), 0, false);
/*
continue the program
*/
and the background.php:
session_id($argv[1]);
session_start();
sleep(5);
$test = $argvs[1];
$myFile = "myFile.txt";
$fh = fopen($myFile, 'w') or die("can't open file");
fwrite($fh, $test);
fclose($fh);
The background process creates the myFile.txt however the session variable doesn't work. I did some other tests and it doesn't work in any case. Anyone knows why?
Is it a limitation of using a background process?
I edited the code, my problem now is that I can't pass any variable as arguments. $argv is always empty.
I finally solved it, register_argc_argv must be enabled on php.ini!
You can pass session_id
to the background script:
$oExec = $WshShell->Run("C:/xampp/php/php-cgi.exe -f C:/xampp/htdocs/sand_box/background.php " . session_id(), 0, false);
In your background script, you write as the first line:
session_id($argv[1]);
session_start();
Edit: As mentioned by @chris, due to locking you need to be aware that the background script will be waiting for the index.php
to stop executing.
The PHP process invoked by COM will have no knowledge of the user's already established session. Instead you should try passing the session value as an argument to the background.php
script:
$oExec = $WshShell->Run(sprintf("C:/xampp/php/php-cgi.exe -f C:/xampp/htdocs/sand_box/background.php %s", $_SESSION['test']) , 0, false);
Then in your background.php
you should be able to access the value via $argv
:
// You should see the session value as the 2nd value in the array
var_dump($argv);
$myFile = 'myFile.txt';
....
The above is just a theory as I haven't run it through COM
before, but should work.
More on argv here.
-- Update --
session_start();
$WshShell = new COM("WScript.Shell");
// Notice the space between background.php and session_id()
$oExec = $WshShell->Run("C:/xampp/php/php-cgi.exe -f C:/xampp/htdocs/sand_box/background.php " . session_id(), 0, false);
php usually gets the session id from a cookie or in an http request field. When you execute via command line directly, neither are available to it. So, consider passing the session_id() via a command line arg or environment variable, and then specify it in your spawned script via
session_start($the_session_id);
next, you need to make sure this other instance of php uses the same config. It could use a different setting for session_save_path
. check via phpinfo() and adjust as necessary.
And finally, php uses an exclusive locking model on the session file. So, only one process can have a session file for a specific session id open at a time. php normally releases its lock on a session file when it finishes executing the script, but you can make this happen sooner via session_write_close(). If you dont call session_write_close() before spawning the other script, the other script will hang in deadlock when session_start($the_session_id);
gets called.
but...if the second script doesnt need to modify the session, dont even bother. just pass it the values it needs and forget the session.