tags:

views:

62

answers:

2

Hey, im using a connection to a server in my php script, opened with fsockopen() and i want it to share between different pages so i serialized it and saved it in a session variable but it seems that that ia a bad idea because when i do this nothing happens... Not even an error. The problem is that this connection requires a handshake so i cant reconnect everytime

Another question, whats the timeout of fsockopen or does the connection stay alive if the. original php script which called it is closed?

EDIT:// I have a script thats running a long time so it would be possible to keep it open, but my initial question, can i share the handle via $_Session and if yes do i need to serialize it? because if i echo the handle it isnt an integer

+3  A: 

You cannot save that in your session data, when the opening PHP script returns, the process "dies" and your socket goes with it. You may save the integer value from your handle, but it will be no longer valid when the next page is loaded and run.

Francisco Soto
Are you sure its in an integer?
Chilln
No I am not, I just wanted to point out that the object you save between pages is meaningless when you the creating page exits.Sorry for the inaccuracy.
Francisco Soto
+2  A: 

fsockopen is opening a network socket.

When the PHP script that opened that socket ends, the sockets opened by it are lost : you cannot re-use them from another script.

If you want several distinct pages to use that socket, there is no other way than re-opening it for each script, even if it takes time.


Another (a lot more complex) solution could be to have :

  • one script that runs as daemon, in background, and connects to the remote service
  • that script is always running (or, at least, for a long time)
  • other scripts send informations to that daemon, instead of trying to connect to the service
  • and the daemon, which is always connected, sends those informations to the remote service it's connected to

It's a bit more complex, as I said... but it should work pretty well :-)

Note, though, that using a daemon and all that will probably require you to have access to the command-line of your server : you will not be able to do (or not that well) all that if you only run PHP from Apache.

Pascal MARTIN