0
    session_start();

    if(isset($_GET['progress'])){
        $status_done = '0';

        if($_SESSION['progress_percentage'] == "100"){
            $status_done = '1';
        }

        echo json_encode(array(
            'progress_percentage' => $_SESSION['progress_percentage'],
            'progress_status' => $_SESSION['progress_status'],
            'progress_done' => $status_done
            )
        );
    }
    elseif(isset($_GET['dummytask'])){
        for ($i = 1; $i <= 100; $i++) {
            $_SESSION['progress_percentage'] = $i;
            $_SESSION['progress_status'] = 'Checking the locks and stuffing the dolls!';
            sleep(1);
        }
    }
        $(document).ready(function(){   
            var timeout = '';

            function checkProgress(){
                console.log('Checking progress event started');

                $.ajax({
                    url: 'http://willetu54.fiftyfour.axc.nl/dgi/ajax.php?progress&time='+ new Date().getTime(),
                    cache: false,
                    dataType: 'json'
                }).done(function(data) {
                    $('#progressbar-progress').html(data.progress_status +'('+ data.progress_percentage +')');

                    console.log('Progress checked');
                    console.log(data);

                    if(data.progress_done == "1"){
                        clearTimeout(timeout);
                    }
                }).fail(function(jqXHR, status, error){
                    console.log('Check Progress: Error: '+ error + status +'');
                });

            }

            function checkProgressTimeout(){
                checkProgress();
                console.log('Timeout tick for progress');

                timeout = setTimeout(checkProgressTimeout, 500);
            }

            $('#menu-open').click(function(event){
                checkProgressTimeout();

                console.log('starting dummy task');

                event.preventDefault();

                $.get('http://willetu54.fiftyfour.axc.nl/dgi/ajax.php?dummytask', function(data) {
                    alert('Load was performed.');
                });
            });
        });

Hello again Stackoverflow!

The system I'm currently developing has scripts that run quite slow and can last up to 20 seconds sometimes. (Creating large amounts of folders, moving large amounts of files, etc). So thats why I've designed this script. I've created a small dummytask wich just lasts ~100 seconds. When I fire that script using AJAX it completes a certain amount of tasks. Every time a task completes I update my $_SESSION variable with the scripts' progress and load that onto my page with another jQuery script.

But it works out kinda strange. I think it does launch the checkProgressTimeout and the dummy task (With the checkProgressTimeout function it checks the progress every 500 ms), But when I look in console I see it only fires the event but does not get any results untill the dummy task is completed and then it slowely returns the progress checking with the same timeval (500ms).

This problem occurs in Google Chrome. The script doesn't work at all in IE.

I've set up a jsFiddle: http://jsfiddle.net/nZAs4/2/. But since jsFiddle does not support PHP scripts I've taken the privilege to upload it myself to one of my own webhosts. (I've also allowed Access-Control-Allow-Origin).

So how can I get this to work?

Thew
  • 15,789
  • 18
  • 59
  • 100
  • `$status_done = '0';` So you add the string `0` which is no integer! `String + String = StringString`. `parseInt(StringString) = NaN (Not a Number)` – Ron van der Heijden Jul 16 '13 at 10:09
  • 1
    your session data will only be available when the script has finished. you should consider updating a database instead and closing the session for writing to allow another script to run concurrently `session_write_close()` http://php.net/manual/en/function.session-write-close.php – Waygood Jul 16 '13 at 10:09
  • @Bondye Can you explain that? I'm not following along. – Thew Jul 16 '13 at 10:18
  • You assign the string `'0'` instead of the int `0` to `$status_done` which sound wrong :) Even I would use `$status_done = true;` – Ron van der Heijden Jul 16 '13 at 10:38

2 Answers2

1

By default, your session variables are not updated until the script terminates. You'll have to call session_write_close() in order to persist the data.

n3rd
  • 5,989
  • 4
  • 39
  • 56
  • Where do I put the `session_write_close()`? Do I have to put it underneath session_start(), or do I use it after or before I updated the session? The documentation on PHP.net doesn't inform me on this... – Thew Jul 16 '13 at 10:16
  • Whenever you wrote data to the session and want that data to be available to other scripts. You'' probably have to reopen the session before you can close it again, though. – n3rd Jul 16 '13 at 10:25
1

Since N3rd decided to copy and paste here's mine:

By default, your session variables are not accessible to other scripts until the current script terminates. You'll have to call session_write_close() after you have finished processing the session data in order to persist the data.

This can be done as follows:

  1. start the session
  2. create db entry for this running script
  3. update you session to record the index of this record
  4. close your session for writing session_write_close();
    • this allows your monitoring script to start working
  5. run the processing part of the script (that takes ages) WHILE updating the db entry with its progress

on the monitoring script:

  1. start session (this will hang until the same session in another script has finished/write-closed)
  2. check session for index to job
  3. get the data from the database
  4. show progress
  5. close session
Waygood
  • 2,657
  • 2
  • 15
  • 16
  • But using databases, won't that add serveral hundreds of miliseconds to the script? – Thew Jul 16 '13 at 10:22
  • 1
    depends how often you update/check it. Since this is the method I use on a 10 minute+ script a few milliseconds is negligible – Waygood Jul 16 '13 at 10:24
  • Well as I said scripts will last up to 20 seconds, sometimes even more. – Thew Jul 16 '13 at 10:25
  • 1
    Okay, an alternative non-DB approach would be to `session_start(); $_SESSION['progress']=#percentage#; session_close();` when you update the session variable (but I haven't tested this) – Waygood Jul 16 '13 at 10:31
  • The final (working) non-DB solution for me is to during the tasks, after updating the session, calling `session_write_close()` and after that calling `session_start()` again. – Thew Jul 16 '13 at 10:35
  • @Waygood, hello. Could I kindly ask you to provide me with information, how have you implemented your monitoring script. I simply can not make it work. Please, [see my question](http://stackoverflow.com/questions/19692282/php-script-in-iframe-blocks-other-code), with all the code samples. – Bunkai.Satori Oct 31 '13 at 00:45