views:

507

answers:

3

Hi all!

I'm making an MSN client in PHP. I have this code, which connects to a server and logs in to MSN:

$server2 = explode(":", $xfr[3]);
$socket2 = pfsockopen($server2[0], (int)$server2[1]);
$_SESSION["socket"] = $socket;
echo '<b>Connected to 2nd server.</b><br />';
//Send MSNP version
fputs($socket2, "VER 0 MSNP10 CVR0\r\n");
echo fread($socket2, 5000) . '<br />';
//Send user-agent
fputs($socket2, "CVR 1 0x0409 php ".phpversion()." i386 MSNMSGR 7.0.0000 MSMSGS ".$_POST["username"]."\r\n");
echo fread($socket2, 5000) . '<br />';
//Send username
fputs($socket2, "USR 2 TWN I ".$_POST["username"]."\r\n");
//Read USR
$usr = fread($socket2, 5000);
echo $usr . '<br />';
$usr = explode(" ", $usr);//This is for later usage.

Now I need to use this socket in another page (AJAX/status.php). php.net says that the connection stays available. However, this is status.php (just ignore $_SESSION["cid"]), which is called via AJAX:

<?php
session_start();
fputs($_SESSION["socket"], "CHG 12 " . $_GET["s"] . " " . $_SESSION["cid"], 5000);
echo fread($_SESSION["socket"]);

Which should change the status. I get this error:

<br />
<b>Warning</b>:  fputs(): supplied argument is not a valid stream resource in <b>C:\wamp\apps\msnphp\AJAX\status.php</b> on line <b>3</b><br />
<br />
<b>Warning</b>:  Wrong parameter count for fread() in <b>C:\wamp\apps\msnphp\AJAX\status.php</b> on line <b>4</b><br />

My socket is persistent, and it's id is saved in session variable $_SESSION["socket"]. I do not understand why it does not work.

I use Windows XP Professional SP2 as server, with WAMP (Apache, MySQL and PHP) server.

Can anyone help me? Thanks!

+2  A: 

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.

Tom Haigh
A: 

Hi,

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

moorthi daniel
A: 

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

roberto