fsockopen() and session variables
Moderator: General Moderators
fsockopen() and session variables
Hi,
I have 1.php and 2.php. In 1.php, I used fsockopen() to call 2.php. I noticed that the session variables in 1.php couldn't access by 2.php. I guess that since I used fsockopen() so 2.php and 1.php were not in the same session. How can I call 2.php so it is in the same session as 1.php? Note that I have to use fsockopen().
Thank you.
I have 1.php and 2.php. In 1.php, I used fsockopen() to call 2.php. I noticed that the session variables in 1.php couldn't access by 2.php. I guess that since I used fsockopen() so 2.php and 1.php were not in the same session. How can I call 2.php so it is in the same session as 1.php? Note that I have to use fsockopen().
Thank you.
Thanks. Those are in the same server. I may write to file or database. However, I wonder if we can use something "above" the scope of SESSION variable. Since I am new to PHP so I'm not sure if in PHP there is some kind of variable which can share among sessions (just like APPLICATION in ASP). Or if we know the session id of 1.php, can we pass it to 2.php so 2.php can use the session variable of 1.php?
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
Well you can pass the session id vai the URL. It's stored in the constant SID so something like:
Code: Select all
$url = 'http://localhost/2.php?'.SID; //Append session IDFirst, there is no APPLICATION analog in PHP.newerbie wrote:Thanks. Those are in the same server. I may write to file or database. However, I wonder if we can use something "above" the scope of SESSION variable. Since I am new to PHP so I'm not sure if in PHP there is some kind of variable which can share among sessions (just like APPLICATION in ASP). Or if we know the session id of 1.php, can we pass it to 2.php so 2.php can use the session variable of 1.php?
Second, d11wtq's suggestion should work. If you've appending the SID, you should be able to use $_SESSION to get at the shared session variables. There might be a few "race conditions". Have you tried it? Can you show the source code for 1.php and 2.php?
Yes it works. I now can get 2.php access session variable of 1.php. See my 1.php and 2.php below. However, I faced with another problem: value of $_SESSION['var1']set by 2.php only take affect after 1.php finished! Even that 1.php have been wait 5s after 2.php to be completed. Please advice how can I get the value of session variable affected immediately?
Run 1.php at the first time, the output is:
Run 1.php at the second time, the output is:
Code: Select all
//1.php
<?php
session_start();
echo "e;Var 1 = "e;.$_SESSIONї'var1']."e;<br>"e;;
//function to call a page by sock
function execURL($domain, $port, $path) {
$fp = fsockopen($domain, $port, $errno, $errstr, 30);
if (!$fp) {
echo "e;$errstr ($errno)<br />\n"e;;
} else {
$out = "e;GET "e;. $path ."e; HTTP/1.1\r\n"e;;
$out .= "e;Host: localhost\r\n"e;;
$out .= "e;Connection: Close\r\n\r\n"e;;
fwrite($fp, $out);
fclose($fp);
}
}
$_SESSIONї'var1']=''; //set it blank to see how 2.php affect it
execURL("e;localhost"e;,80, "e;/2.php?PHPSESSID="e;.session_id()); //call 2.php
sleep(5); //sleep to wait for 2.php completed
echo "e;Var 1 = "e;.$_SESSIONї'var1']."e;<br>"e;; //however $_SESSIONї'page1'] still blank!
?>Code: Select all
//2.php
<?php
session_start();
$_SESSION['var1'] = "Set by 2.php";
?>Code: Select all
Var 1 =
Var 1 =Code: Select all
Var 1 = Set by 2.php
Var 1 =yeah that's the timing issue I was worried about.
You can probably use session_write_close or its alias session_commit after 1.php has set its variables, before the call to 2.php and then called session_start a second time in 1.php after the call to 2.php.
In 2.php, you'll want to call session_commit as early as you can as well.
You can probably use session_write_close or its alias session_commit after 1.php has set its variables, before the call to 2.php and then called session_start a second time in 1.php after the call to 2.php.
In 2.php, you'll want to call session_commit as early as you can as well.
Thank nielsene. It works. However, I learned another problem: if in 1.php I called 2.php and 3.php without transering sessionid, 2.php and 3.php will run in parallel. If I called 2.php and 3.php with sessionid (meaning that they are in the same session), 2.php and 3.php will run in serial. I guess that this is because PHP has only one thread for each session? Is there any way to have 2.php and 3.php run in parallel in the same session?