two dimensional $_SESSION in php
I'm building a server side workers manager script which saves the "jobs to work on" data in the $_SESSION and each time calls itself with the next job to be done. This gives me control, because I can stop and re-continue whenever I need. This works great. The problem I'm having is to be able to run a couple of these Managers in the same browser because they share the $_SESSION. So I'm trying to create a two-dimensional $_SESSION array that keeps different data according to the sessionId I specify in the http request. I want to do this in order to run a number of crawlers from the same browser that do one task at a time and keep a list of remaining tasks in its' $_SESSION[$sessionId]
variables. So I've added a sessionId
to each request and then each script uses it. It works great without the sessionId
and doesn't work as I expect with. I'm having a hard time understanding why. This is a sample code to demonstrate my problem:
session_start();
$sessionId = $_REQUEST['sessionId'];
if ($_REQUEST['currId'] == 0) {
unset ($_SESSION[$sessionId]);
$_SESSION[$sessionId]['log'] = "starting log for session " . $s;
}
if ($_REQUEST['currId'] < $_REQUEST['limit']) {
$_SESSION[$sessionId]['log'] .= " adding ";
// call with next currId
开发者_开发百科 echo '<META HTTP-EQUIV="Refresh" CONTENT="1;URL=' . "'test3.php?limit=" . $_REQUEST['limit'] . "&currId=" . ($_REQUEST['currId']+1) . "&sessionId=" . $sessionId . "'" . '">';
}
// reached limit
else {
echo $_SESSION[$sessionId]['log'];
};
I'm invoking first a call to test3.php?limit=5&currId=0&sessionId=0
and than expect the code to re-call itself with currId=1,2,3,4,5. This is happening Ok. but the $_SESSION isn't kept as I
expect it to be. When I remove the sessionId
usage, it does work. But that way I can only invoke one crawler simultaneously unless I open a different browser...
Thanks
You might have more luck with a sessionId
that isn't a number. If you turn error reporting up with error_reporting
(E_ALL)
; you'll see some notices, one of which is Notice: Unknown: Skipping numeric key 0 in Unknown on line 0
.
According to one commenter on the php pages this is caused by using an integer as a key for the $_SESSION
array - when I tried this without the error disappears.
You have also got a variable $s
in there, which I assume should be $sessionId
, that'd give a notice too.
精彩评论