Search code examples
phpexecution

Closing browser window when php is in execution


what exactly is happening when PHP is putting contents (1 MB for example) in a file and the user closes the tab? Could the file be corrupted? How can I avoid that? Thanks a lot


Solution

  • This is more a client-side issue...

    I solved this problem using Javascript's window.onbeforeunload and window.onunload functions:

    <head>
    <script>
    var invalid_exit=0;
    var submitted=0;
    
    window.onbeforeunload = function(){ 
        if (invalid_exit==1  && submitted==0 ){
            return 'You will lose your progress!';
        }
    }
    
    window.onunload = function(){ 
        if (invalid_exit==1  && submitted==0 ){
        //ajax call when an invalid exit is happening...        
        }
    </script>
    </head>
    

    In this case, I used to control variables: invalid_exit and submitted....

    You can check if a user clicks a submit button(submitted=1), closes the browser(invalid_exit=0), refreshes the page(invalid_exit=0), etc... Depending on your requirement....

    With Jquery you can control every event...

    I hope this can help you out!!!