简体   繁体   English

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

[英]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 . 在重定向到index.html之前,我需要运行一个index.php脚本,该脚本调用一个需要花费几秒钟运行的外部脚本。 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? php脚本是否可以同时将相同的index.html加载到浏览器中,以使屏幕不出现空白? 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"> index.html的这一行是- <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. 这避免了shell_exec阻塞处理的问题。

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. 如果您还发送内容,则可能需要发送Content-Length标头。 If you're using sessions be sure to close the session with session_close() . 如果您正在使用会话,请确保使用session_close()关闭会话。 You should also flush any output buffers. 您还应该刷新所有输出缓冲区。


The DOMDocument error you're getting is unrelated. 您收到的DOMDocument错误是无关的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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