Page 1 of 1

fsockopen() and session variables

Posted: Sat Jul 23, 2005 1:28 am
by newerbie
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.

Posted: Sat Jul 23, 2005 1:37 am
by nielsene
Are the two files on the same server? If yes, you might be able to write the variables you want to share to a temp file somewhere.

Otherwise, you might be able to do something using the cURL library to manually set some extra headers, but I'm not sure that would work.

Posted: Sat Jul 23, 2005 7:03 am
by newerbie
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?

Posted: Sat Jul 23, 2005 9:36 am
by Chris Corbyn
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 ID

Posted: Sat Jul 23, 2005 9:59 pm
by newerbie
Thanks. But how can I get 2.php access the session variable of 1.php?

Posted: Sat Jul 23, 2005 11:20 pm
by nielsene
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?
First, there is no APPLICATION analog in 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?

Posted: Sun Jul 24, 2005 9:06 am
by newerbie
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?

Code: Select all

//1.php
<?php
session_start();

echo &quote;Var 1 = &quote;.$_SESSIONї'var1'].&quote;<br>&quote;;

//function to call a page by sock
function execURL($domain, $port, $path) {
	$fp = fsockopen($domain, $port, $errno, $errstr, 30);

	if (!$fp) {
	   echo &quote;$errstr ($errno)<br />\n&quote;;
	} else {
	   $out = &quote;GET &quote;. $path .&quote; HTTP/1.1\r\n&quote;;
	   $out .= &quote;Host: localhost\r\n&quote;;
	   $out .= &quote;Connection: Close\r\n\r\n&quote;;
	   fwrite($fp, $out);
	   fclose($fp);
	}
}	

$_SESSIONї'var1']=''; //set it blank to see how 2.php affect it

execURL(&quote;localhost&quote;,80, &quote;/2.php?PHPSESSID=&quote;.session_id()); //call 2.php

sleep(5); //sleep to wait for 2.php completed

echo &quote;Var 1 = &quote;.$_SESSIONї'var1'].&quote;<br>&quote;; //however $_SESSIONї'page1'] still blank!
?>

Code: Select all

//2.php
<?php
session_start();
$_SESSION['var1'] = "Set by 2.php";
?>
Run 1.php at the first time, the output is:

Code: Select all

Var 1 =
Var 1 =
Run 1.php at the second time, the output is:

Code: Select all

Var 1 = Set by 2.php
Var 1 =

Posted: Sun Jul 24, 2005 10:19 am
by nielsene
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.

Posted: Tue Jul 26, 2005 10:42 pm
by newerbie
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?