开发者

I want the php page to load first with other information & then do an operation

开发者 https://www.devze.com 2023-01-31 08:07 出处:网络
I am looking for a solution so that the php page will load all other information in it say the contents/images if any and then do an operation which will take some time to complete and show its result

I am looking for a solution so that the php page will load all other information in it say the contents/images if any and then do an operation which will take some time to complete and show its results.

As the operation is taking long time the php page loads too slow. I want my client to saw the page first and then do the operation which at the end will show result.

I tried with the ob_start() & flush() but was unsuccessfully. It may need Javascript to ini开发者_开发问答tial the operation I think so. But how can I impliment this.


Is that a php opperation? Since php is a serverside language it is executed before the user receives the http response. So you would have to build the site first. Send that to the user. Than make a new request (For example with ajax) to the script that needs to do the operation. This is the order code should be executed.

  • PHP(Serverside): Build your website, main php code is executed
  • HTML(Clientside): HTML is build, images and javascripts files will be loaded
  • Javascript(Clientside): Create a new httprequest(asynchronous) to a php file with your opperation. Be aware of timeouts because of the long time the opperation can take. Set a - custom timeout time if needed.
  • PHP(Serverside): Execute the long opperation and return the result (Make sure your php server doesn't timeout, think default is 30 seconds for most servers)
  • Javascript(Clientside): Do something with the result in the ajax handler.

Some handly links.

Ajax tutorial of w3schools
jQuery ajax api with examples


jQuery makes this incredibly easy, as such...

<script type="text/javascript" src="jquery-1.4.4.min.js"></script> <!-- Put this in your web page's head -->
<script type="text/javascript">
$(function() {
    $('wherever-you-need-the-resulting-html-to-go').html('<img src="loading-image.gif" alt="Loading" />');
    $.ajax({
        url: 'wherever-your-slow-code-is.php',
        success: function(msg) {
            $('wherever-you-need-the-resulting-html-to-go').html(msg);
            }
        });
    });
</script>


You'll need to load the page without the "slow call". In either the onload event or in a script at the end of the web page, do an XHR call to the server to get the slow data. The call is asynchronous by default so the user can interact with the webpage while waiting for the call to return. At that point you would grab the data and convert it into HTML with JavaScript.


You use onload, to trigger a timed event in a very short time. That will let the screen be drawn.

When the event fires, start your long process using AJAX techniques.

You cannot attempt to run a process on the server for a long time. After a while (typically 300 seconds) a run-away timer will kill your server thread. To avoid this, your long process must either use a polling techneque or not run under a normal web server environment.

If it is going to run for more than 20 or 30 seconds, then save some form of status in the session, and return a "working" reply. Use the reply to update the user on progress, before fireing off the next cycle. This loads and positions itself usign the session data, and runs for another short period of time.

Warning - php will carefully serialise all access to the session data, so any send by the user while the server is processing a cycle will wait for that cycle to end before it is handled.

0

精彩评论

暂无评论...
验证码 换一张
取 消