会话变量不适用于php中的后台进程

问题描述:

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!

会话变量在使用后台进程时是否有效? p>

我有两个php scripts - index.php: p>

  session_start(); 
 $ _SESSION ['test'] ='test'; 
 
 $ WshShell = new COM(“WScript  .Shell“); 
 $ oExec = $ WshShell->运行(”C:/xampp/php/php-cgi.exe -f C:/xampp/htdocs/sand_box/background.php“.session_id(),  0,false); 
 / * 
继续程序
 * / 
  code>  pre> 
 
 

和background.php: p>

  session_id($ argv [1]); 
session_start(); 
 
sleep(5); 
 
 $ test = $ argvs [1]; 
 
 $ myFile =“myFile.txt  “; 
 $ fh = fopen($ myFile,'w')或死亡(”无法打开文件“); 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
($ fh,$ test); 
 nclclose($ fh); 
  code  >  pre> 
 
 

后台进程创建myFile.txt但是会话变量不起作用。 我做了一些其他测试,但在任何情况下都不起作用。 有人知道为什么吗? p>

这是使用后台进程的限制吗? p>

我编辑了代码,我现在的问题是我不能 将任何变量作为参数传递。 $ argv总是空的。 p>

我终于解决了它,必须在php.ini上启用register_argc_argv! p> div>

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.