简体   繁体   中英

PHP to load an HTML page while running an external script

I need to run an index.php script that calls an external script which takes a few seconds to run, before redirecting to index.html . Is it possible for the php script to load the same index.html into the browser in the meantime so the screen isn't blank? If not what would be the way to handle this? My best effort is:

<?php
    $doc = new DOMDocument();
    $doc->loadHTMLFile("index.html");
    echo $doc->saveHTML();
    shell_exec('./shellscript.sh');
    header('Location: index.html');
    exit;
?>

which fails to load with:

Warning: DOMDocument::loadHTMLFile(): Tag nav invalid in index.html, line: 33 in {pathto}/index.php on line 3

That line of index.html is - <nav class="navbar navbar-inverse navbar-fixed-top">

Grateful for assistance.

This is certainly possible. You can close the connection and run the command afterwards. This avoids the problem of shell_exec blocking processing.

It's a little bit tricky to get it to work but it's something like:

 ignore_user_abort(true); // prevent the PHP script from being killed when the connection closes
 header("Location: /index.html");
 header("Connection: Close");
 flush();
 // the connection should now be closed, the user is redirected to index.html
 shell_exec('./shellscript.sh');

If you're also sending content you might need to send a Content-Length header. If you're using sessions be sure to close the session with session_close() . You should also flush any output buffers.


The DOMDocument error you're getting is unrelated.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM