PHP pfsockopen in a session

心不动则不痛 提交于 2019-11-29 08:06:47

pfsockopen() returns a resource. You cannot store resources in the Session as they are just handles to external resources which may not be there later.

If you request the same page again you might get to reuse the connection by calling pfsockopen() again with the same parameters, but I don't think you have any guarantee of this, and it probably won't be practical as for this you probably want one connection per user session.

You could start background PHP processes which connect to the remote server, and read/write events into a queue (maybe a database or memcached). You'd have to make sure these processes are terminated properly otherwise you could quickly have a lot sitting there. Your front-end PHP script can then just read/write from/to the queue.

The problem you have is really based on HTTP being stateless, but the service you are connecting to being stateful. So you have to somehow maintain state (for the external resource) on your webserver, which is not something that is very easy to do with PHP.

Change this:

$socket2 = pfsockopen($server2[0], (int)$server2[1]);
$_SESSION["socket"] = $socket;

for this!

$socket2 = pfsockopen($server2[0], (int)$server2[1]);
$_SESSION["socket"] = $socket2 /* WITH "2" */;

;)

Hi you can create a server persistans conx and just open new conx with the prev set parameters in nexus and finality use js to save some part this .. the style of development is not important. Tnks

Moorthi Daniel

We can do this by writing a class to connect, read, write & disconnect using pfsockopen() and creating and storing the object of that class in a session variable. While storing the object you must serialize it and when you need it back unserialize it. The session variables can store only string data.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!