Jump to content

Variables Between PHP Files


ChristoffMason

Recommended Posts

Hi Guys,

              I am relatively new to php, and have hit a problem.  Basically I am writing a PHP socket server - which acts as a socket server for a swish movie.  The only way of getting data into the swish movie is one function - loadvariablesnum() - which uses 'post' or 'get'.  The swish movie is a web GUI for a system and therefore needs a reasonably decent refresh rate - 5 times a second for example.  Originally I wrote a php file that created / bound / listened to the socket and then entered an infinite while loop that would read from the socket and echo the output to the swish movie.

 

The problem with this method, was that each time the swish movie called loadvariablesnum() the PHP script would re-open - re-bind - re-listen to the socket - which caused all sorts of problems for my client.  Therefore I decided to re-write the PHP file that included functions:

 

<?               //TCP_Socket_Server.php
global $output;

function TCP_Socket_Server()
{
$host = "10.10.13.59";
$port = 10000;
set_time_limit(0);
$socket = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create socket\n");
$result = socket_bind($socket, $host, $port);// or die("Could not bind to socket\n");
$result = socket_listen($socket, 3) or die("Could not set up socket listener\n");
socket_set_block($socket);
$spawn = socket_accept($socket) or die("Could not accept incoming connection\n");
// read client input
while(1)
{
	$input = socket_read($spawn, 10000);// or die("Could not read input\n");
	$input = trim($input);
	global $output;
	$output = $input;
	WriteData();
	flush();
}
socket_close($spawn);
socket_close($socket);
}

function WriteData()
{
static $LocalTemp;
global $output;
$LocalTemp = $output;
$TempBuffer = "&TCP_SocketServerBufferVar=";
//$TempBuffer .= $GLOBALS['output'];
$TempBuffer .= $LocalTemp;
echo $TempBuffer;
}
?>

Using this method I can call TCP_Socket_Server() only once at the beginning of the swish movie using the loadvariablesnum() (from another PHP file), which sets the server running - e.g.

 

<?    //Load_Server.php
include 'TCP_Socket_Server.php';
TCP_Socket_Server();
?>

 

My plan was to then call the WriteData() function regularly approximately 5x per second, (again from another PHP file using loadvariablesnum() in swish):

 

<?    //Write_Data.php
include 'TCP_Socket_Server.php';
Write_Data();
?>

 

However, when I call this function from an external PHP file - no valid data is output by echo - even though I can see the data being received (in the browser) in the main Load_Server.php page - which is output using the same Write_Data() function.

I have tried many different ways of accessing this variable ($output) from a different php file - e.g returning the variable from Write_Data() - which had the same result (i.e. no valid data).

 

I have also tried using sessions, but could only seem to get them to work in files that did not run indefinitely - e.g. without the infinite while loop in TCP_Socket_Server().

 

I am currently running the PHP files (and the rest of the web content) on APACHE2 - in Ubuntu.

 

I would really appreciate any advice on this - as at the moment, this looks like a show stopper - and would waste 2 months work.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.