2

I'm new to php and I want to control a php while loop script, using buttons (start/stop), the start button make an ajax call to the start.php script that define $_SESSION['loop'] = TRUE and execute the loop, the stop button make an ajax call to the stop.php script that just change $_SESSION['loop'] to FALSE.

Below is my code so far, but when I hit the stop button I became the alert (success stop) only after the while loop finish looping, which mean the loop didn't break as I was assuming.

I think it's something with the session that is locked while the loop is executing. If so, how to change the $_SESSION['loop'] value and make the loop read that value each time?

index.php

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
    $(document).ready(function(){
        $('#start').click(function(){
            $.ajax({
                url: "start.php"
            });
        });

        $('#stop').click(function(){
            $.ajax({
                url: "stop.php",
                success: function(){
                    alert('success stop');
                },
                error: function(){
                    alert('failure stop');
                }
            });
        });
    });
</script>

<button id="start">Start</button>
<button id="stop">Stop</button>

start.php

<?php
    session_start();
    $_SESSION['loop'] = TRUE;

    ini_set('max_execution_time', 300);
    $i = 0;
    while($i < 100) {

        if ($_SESSION['loop'] == TRUE) {

            //query to save some variables

            // Pause 10s after saving 2
            if ($i != 0 && $i%2 == 0) {
                sleep(10);
            }

            $i++;

        } else {
            break;
        }
    }
?>

stop.php

<?php
    session_start();

    if(isset($_SESSION['loop'])) {
        $_SESSION['loop'] = FALSE;
    }
?>
3
  • 1
    yes, your loop will not stop after it complete or till the PHP execution time ends, also, the new SESSION value will not be available to the page in the middle of the process... therefore, you can't do something like that Commented Mar 25, 2013 at 13:23
  • @Akam, any suggestion on how to achieve that? Commented Mar 25, 2013 at 13:30
  • 1
    Use a database storage system instead, and keep re-checking inside the loop. IE every if($_SESSION['loop'].. should be a sql select query and $_SESSION['loop']=.. should be an sql update query Commented Mar 25, 2013 at 13:34

3 Answers 3

1

Your start and stop operations are in two different sessions, so changing $SESSION in one makes no difference to the other.

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

3 Comments

Can I use something like cookies to access the same session so I can change its variable?
Cookies live in the browser and are exchanged when a page is loaded, so your loop will never see any changes.
@grahamj42: How do you know they are in two different sessions?
0

I think, you should use multi-threading for doing such things.

Comments

0

Please take some time to read it:

"Session data is usually stored after your script terminated without the need to call session_write_close(), but as session data is locked to prevent concurrent writes only one script may operate on a session at any time."

http://php.net/manual/en/function.session-write-close.php

Hence, it appears that $_SESSION['loop'] is written to session file at the end.

If you want to explicitly write data to session file, add session_write_close after $_SESSION['loop'] = TRUE. Note, if you need to read/write some session data again in the same script, you need to start the session and then read/write data.

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.