Jump to content

WAY HARD. Getting user input WHILE a php script is running.


artacus

Recommended Posts

Can any one think of a solution to this?

 

Here's the problem I'm currently working on, so you can see a possible application.

I have a long running script that I wrote to automate a very tedious process of logging into a telnet session running a terminal application, running several reports then downloading them.

 

I'm using ob_implicit_flush(); so that I can watch the conversation between the php server and the telnet server as it happens. I have it programmed so it listens for a phrase, fires off a response and then waits for the next sequence. But occasionally things will get off and you'll be stuck waiting for a response or something.

 

So I was thinking it would be cool to communicate with a script while it was executing. My initial thought was that maybe javascript could connect to a socket and pass along your keystrokes. Or maybe send a response using Ajax, but how would your running script be able to see that?

 

Anyone have any thoughts? ... besides its impossible?

afaik, php does not operate the way you need it to. php runs the script, delivers whatever output is needed, and then stops running. if you want something that was realtime, i'd recommend integrating a little AJAX into your script.

I have a few minutes to kill before taking off for the day, so I'll expand on my previous post.

 

I'm assuming your long-running script is in a tight loop.  Each pass through the loop it checks a messaging table within the database for new messages since the last time it checked.  It must process any messages it finds and then continue with it's tight loop.  You might even want this script to post messages back to the database.

 

Now you have an extra script that is accessible through a web browser.  When the page loads, it checks for new messages since the page was last loaded from the long-running script.  This page has a form that when posted, sticks new messages into the DB for the other script to process.

 

You could get very fancy with this using AJAX.

 

Alternatively, you could also just have your web page script's form handler serialize the post array and just stick that in the database for the other script to unserialize and process.

 

 

@boo_lolly, you are correct in how PHP works.  But what he is attempting can be done purely with PHP.

 

This is no different than any real-time system that listens and responds to external events.  In this case, you listen to the events in the database and they're posted by another script running elsewhere.

Hadn't thought about using mysql. I was thinking something more real-time like sockets. Not because mysql wouldn't work, I think it will, but I want to push the edges of what php can do.

 

The php script would only have to check for user input when it times out waiting for a specific response. Right now you call:

waitFor($socket,'Text to find', $is_hex, $timeout)

 

If it doesn't find a match in the timeout period, it calls itself 2x more and on the 2nd time it fires off a generic \r and moves on the the next wait/respond sequence.

 

So now I'm thinking... when I start the script, I should be able to start listening on an unprivileged port on the server. Does anyone know if Javascript can connect to sockets? or could I some how send that text to the port using ajax?

Does anyone know if Javascript can connect to sockets? or could I some how send that text to the port using ajax?

 

Sure.  AJAX just uses javascript to asynchronously send an HTTP request.  There is no reason why you can't do something like this:

 

// javascript
receiverXMLHttpObj.open('GET','www.someaddress.com:9898',true);
receiverXMLHttpObj.send(null);

 

Where 9898 is the port that your php daemon is listening on.  This isn't very secure, but there is nothing stopping you from using the HTTP protocol to talk to your daemon.

 

Best,

 

Patrick

Archived

This topic is now archived and is closed to further replies.

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