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

时间:2015-08-20 15:53:01

标签: php

在重定向到index.php之前,我需要运行一个调用外部脚本的index.html脚本,该脚本需要几秒钟才能运行。 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">

感激帮助。

1 个答案:

答案 0 :(得分:1)

这当然是可能的。您可以关闭连接并在之后运行命令。这避免了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错误无关。

相关问题