Output loading message in php

筅森魡賤 提交于 2019-12-13 03:44:35

问题


Problem:

Before I perform a very long computational process, I would like to output a loading message to the user.

Code:

<html>        
<!doctype html>
            <head>
                <title>Loading</title>
            </head>

    <?php

    function longProcess()
    {
       //where long process takes a long time to compute
    }

    ?>
            <body>
                <?php
                        echo <div id = 'loading'> LOADING </div>
                        echo '</body>';
                        echo '</html>';
                        flush();
                        longProcess();

                ?>

Further Details:

Basically, before I call the longProcess function, I wish to output LOADING to the user. I use the flush so I can output the closing tags of HTML, and then call the longProcess function. The problem is that it is not being output and the server is computing the long process.


回答1:


What you need is probably a comet-like feature. Try the following to make it work.

<?php
// Disable buffering
@apache_setenv('no-gzip', 1);
@ini_set('zlib.output_compression', 0);
@ini_set('output_buffering', 'Off');
@ini_set('implicit_flush', 1);
// Flush buffers
ob_implicit_flush(1);
for ($i = 0, $level = ob_get_level(); $i < $level; $i++) ob_end_flush();
?><!DOCTYPE html>
<html>
<head>
  <title>Loading</title>
</head>
<body>
  <div id="loading">LOADING</div>
<?php
// We need to send enough junk messages to make it works for all browsers
echo str_repeat(" ", 1024), "\n";

ob_start();
// Long process starts here
// For this example, just sleep for 5 seconds
sleep(5); 
echo "Loaded";
// Flush output like this
ob_flush();
flush();
?>
</body>
</html>

References:

  • http://jsjoy.com/blog/197/simple-php-comet-example
  • Why doesn't this PHP code (comet) work?
  • http://php.net/manual/en/function.flush.php#109239



回答2:


PHP is server-side. You can't output a loading message.

You can try to make a page (place your "loading" message in this page) that redirects automaticly to your target page. Now you see the loading-page until your target-page is loaded




回答3:


You could possibly output JavaScript using the PHP. You would perform the following:

  • Before longProcess(), output JavaScript to set a #contents DIV within <body> to be a loading animation
  • Run longProcess()
  • Output JavaScript to set the #contents DIV within <body> to output whatever you want, be it data, "finished" or whatever.

Just a quick idea, and almost certainly not the nicest, but it'll work.


An alternative is to re-direct using PHP to a finished page, once the loading process has finished using header.

header('Location: http://www.example.com/');

Finally, Xiao's disable buffering is something you should take a look at!




回答4:


Instead of computing in the same page,

  1. load the page with loading symbol
  2. Initiate the on load ajax call to your PHP file and do all your computation
  3. On response of the ajax call make your output replacing your div of loading.


来源:https://stackoverflow.com/questions/14120084/output-loading-message-in-php

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!