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?

4 Answers 4

1

You can't pass resources between pages.

see PHP pfsockopen in a session

Sign up to request clarification or add additional context in comments.

1 Comment

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
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"];

1 Comment

Have you gotten this to work? For me it doesnt work with resources, only strings
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.

Comments

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..

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.