fsockopen() and session variables

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
newerbie
Forum Newbie
Posts: 5
Joined: Sat Jul 23, 2005 1:15 am

fsockopen() and session variables

Post 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.
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post 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.
newerbie
Forum Newbie
Posts: 5
Joined: Sat Jul 23, 2005 1:15 am

Post 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?
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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
newerbie
Forum Newbie
Posts: 5
Joined: Sat Jul 23, 2005 1:15 am

Post by newerbie »

Thanks. But how can I get 2.php access the session variable of 1.php?
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post 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?
newerbie
Forum Newbie
Posts: 5
Joined: Sat Jul 23, 2005 1:15 am

Post 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 =
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post 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.
newerbie
Forum Newbie
Posts: 5
Joined: Sat Jul 23, 2005 1:15 am

Post 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?
Post Reply