1

EDIT: I can put the question in a shorter way that perhaps focuses more closely on the real problem:

Is there a way to keep PHP Sockets alive over multiple pages? That is, can I store the connection (the socket object) in any way?

I want to save a socket connection between two PHP pages where the other page is loaded via jQuery ajax. I tried it with sessions and so far got this on my main page:

$sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
$res = socket_connect($sock, $host, $port);
session_start();
$_SESSION['sock'] = $sock;  

I then load a page with jQuery:

$("#" + in_name + "_holder").load("set_setpoint.php", {name:in_name, value:in_value});

And in set_setpoints.php:

session_start();     
require_once("sock_funcs.php");    
echo $_SESSION['sock'];

I only get '0', but what I should get is something like "Resource id #xxx". Maybe sessions is the wrong way?

Frank
  • 435
  • 1
  • 9
  • 13

4 Answers4

1

You can't pass resources between pages.

see PHP pfsockopen in a session

Community
  • 1
  • 1
symcbean
  • 47,736
  • 6
  • 59
  • 94
  • So you mean that i need to establish a new tcp connection everytime the other page is loaded? Im new to php but seems so impossibly akward – Frank Jun 13 '11 at 12:38
1

Why don't you use COOKIE to save your value and then access using CLIENT anywhere you want.

1

$_COOKIE["socketNo"] = "somevalue";

Access it like

$_COOKIE["socketNo"];

Make sure the cookie is accessible.

2

You can pass the data as a query-string to the URL that is used on AJAX call and retrieve it in your script.

$("#" + in_name + "_holder").load("set_setpoint.php?socketNo=", {name:in_name, value:in_value});

in set_setpoints.php

$sock = $_GET["socketNo"];

Rakesh Sankar
  • 9,337
  • 4
  • 41
  • 66
0

You can use GET or POST to pass the content, or you can set a cookied encoded and share the content between the two applications.

Gayan Hewa
  • 2,277
  • 4
  • 22
  • 41
0

just try $.get(); in jquery,,

for ur question $.get() or $.post();

if u use $.get(); then u can understand how this parrameter run if any doubt ask me dude..

K6t
  • 1,821
  • 1
  • 13
  • 21