0

My HTML page

<html>
<body>
<form action="myScript.php" method="POST">
    Enter a transceiver ID:
    <br><br>
    <input type="text" name="id" >
    <br><br>
    <input type="submit" value="Submit">
</form>
</body>

My PHP script which is running on my server

<?php
$id_array = array("101");
while(1)
{
    if (isset($_POST['id']))
    { 
        $id = $_POST['id'];
        echo ("ID is ".$id."\n");
        array_push($transceiverID_array, $id);
    }
    print_r($id_array);
    usleep(5000000);
    // lot of processing will be done 
}
?>

My PHP script is in a forever while loop. If I post some data from my HTML page, my PHP script is doing something else (which takes quite a lot of time), and not necessarily processing the line where it is checking if the variable from HTML page is set. So, even if I posted something from my HTML page, due to the long duration between posting data and the PHP script going on the isset line, my PHP script is unable to pick up data.

Is there any time limit in which I need to pick up the variable in my PHP script posted from the HTML page? Is there any better way of doing this?

Also, after posting, my HTML page, keeps waiting for my PHP script to respond. Is there any way to stop loading my HTML page?

SMG
  • 95
  • 1
  • 2
  • 11
  • why do you have a while loop? what do you think it's good for. this while loop will run "infinite" because 1 will always be true – Mart Nov 19 '16 at 09:10
  • `My PHP script is in a forever while loop.` What? That doesnt work in that way. Thing you need a web.socket for that http://stackoverflow.com/questions/7160899/websocket-client-in-php and ajax post in a form – JOUM Nov 19 '16 at 09:10
  • You literally put it in a 'forever while loop' by using `while(1)`. – Rudi Visser Nov 19 '16 at 09:10
  • Yes, I need it to be running forever. Am I doing any mistake? – SMG Nov 19 '16 at 09:12
  • I think its because you make it sleep for 5 minutes after the first time of posting – henrybbosa Nov 19 '16 at 09:12
  • @TariiqHenryBbosa I tried after removing sleep as well, does not work – SMG Nov 19 '16 at 09:13
  • you should not have `while(1)` (and also the `usleep`). your php script will be called every time someone submit your form. You don't need your PHP to be *run all the time* to be able to receive the submit. The web server is the one that "runs all the time" to accept the request and will run your php script. – elfan Nov 19 '16 at 09:15

4 Answers4

0

As the other commentors asked. What is the point of having an infinite loop? The server will do the task of listen for requests in your behalf!

It is natural that the HTML page doesn't receive anything, as the flow in your PHP script never ends. Remove that loop and try the request.

ablurb
  • 56
  • 4
0

The problem is that you get your HTTP Parameters only once every call. If you create a infinite loop you won't get a response from the server. Even your parameter will not change because you PHP Script is called separately every time you do a request.

Why do you need to run forever? Isn't it better to do the work somewhere else e.g starting a cronjob?

CHF
  • 264
  • 4
  • 17
  • I cannot restart my script due to the processing that it is doing. If I use a cronjob, won't my script start from the beginning every time? It will re-initialize all the variables (which I did not show here), which I do not want – SMG Nov 19 '16 at 09:18
  • You can store your variables in a file or database and read it again when you are executing the script. You can for example simply use the Serialize function: http://php.net/manual/de/function.serialize.php – CHF Nov 19 '16 at 09:21
  • Yes it will start from the beginning every time. You would have to handle it to load the content before and continue where you want it to start. The problem with your script is that everytime somebody calls your page the script will start at the beginning too. So you have to serialize and deserialize your data somehow. - Then you have the possibility to continue wherever you want. – CHF Nov 19 '16 at 09:26
  • Do you think using using nodeJS instead of PHP, to do something continuously would be better? – SMG Nov 19 '16 at 09:38
  • I don't know that much about nodeJS. But at the moment you have a problem with the HTTP Protocol. The browser requests data and you have to bring it back to the client. This request should be answered as fast as possible. So it's no good idea to have a infinite loop here. To process the data you need something like a background worker. This could be a cron script, some JS code (in case of nodeJS) or even forever running java or c# code if you are using one of these technologies. To notify the user i'd use client code and Websockets or AJAX requests. You have to overthink your worker code. – CHF Nov 19 '16 at 09:46
0

Your php does have a time limit: http://php.net/manual/en/function.set-time-limit.php

However it is not clear why you want this to loop forever. your while 1 loop will always state true and therefor run "infinitely" this infinity however is limited. (see link above)

Mart
  • 475
  • 4
  • 21
0

EDIT: From this comment: I cannot restart my script due to the processing that it is doing. If I use a cronjob, won't my script start from the beginning every time? It will re-initialize all the variables (which I did not show here), which I do not want

I'm not sure using PHP alone is enough to achieve what you want. You need to consider a persistence layer (memory, database) to keep your variables alive throughout multiple requests. while(1) is not how you do this.


From reading through your post, it sounds like you're misunderstanding HTTP/PHP. It looks as if you're trying to create a loop that will keep "listening" for a POST value to come through from the HTML form as if you're writing a web server itself or something.

That's not how HTTP works.

  • The client will make a GET request to get your HTML, your script then ends.
  • The client will then make a POST request with the data, your script processes it and then ends.

This happens every time, your script will run once per set of POST values that it receives. Note that your HTML isn't "waiting", your HTML was "done" in the GET request, your browser is now making a request to your PHP script and, until you have output, it will not display anything.

Now, you say it's a long running process and you don't care ie. fire-and-forget, where you're saying that you don't In this case, it may be better to simply use an AJAX request that you can fire and not care about the response. Another alternative would be to start a request into an iFrame or similar.

Either way, removing the while loop will allow you to verify that your request is going through, like so:

<?php
    $id_array = array("101");
    if (isset($_POST['id']))
    { 
        $id = $_POST['id'];
        echo ("ID is ".$id."\n");
        array_push($transceiverID_array, $id);
    }
    print_r($id_array);
?>

An option for what you're trying to do, if you want to provide feedback to the browser, is to flush content to the browser as you go.

Try POSTing to this script and see what happens:

<?php
for($i = 0; $i < 5; $i++)
{
    echo $i;
    flush(); // Send processing so far to the browser
    ob_flush(); // For good measure
    sleep(1);
}
Rudi Visser
  • 21,350
  • 5
  • 71
  • 97