PHP在运行外部脚本时加载HTML页面


PHP to load an HTML page while running an external script

在重定向到index.html之前,我需要运行一个调用外部脚本的index.php脚本,该脚本需要几秒钟才能运行。是否有可能的php脚本加载相同的index.html到浏览器在此期间,所以屏幕不是空白的?如果不是,该怎么处理呢?我最大的努力是:

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

无法加载:

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

index.html的那行是- <nav class="navbar navbar-inverse navbar-fixed-top">

感谢您的帮助。

这当然是可能的。您可以关闭连接,然后运行该命令。这避免了shell_exec阻塞处理的问题。

让它工作起来有点棘手,但它是这样的:

 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');

如果你也发送内容,你可能需要发送一个Content-Length头。如果您正在使用会话,请确保使用session_close()关闭会话。您还应该刷新所有输出缓冲区。


你得到的DOMDocument错误是不相关的