【问题标题】:Redirect then execute重定向然后执行
【发布时间】:2016-11-30 09:32:29
【问题描述】:

我正在尝试进入加载页面,然后执行 powershell 脚本。但不幸的是,我无法让它发挥作用。当我转到该页面时,它会自动转到脚本并将我重定向到失败页面的成功。 我试图在重定向后休眠,但它只是等待几秒钟,然后运行脚本并将我重定向到 html 页面。

        header('Location: /loading.html');
        include ("file\where\the\variable\comes\from.php");
        $s = shell_exec("powershell path\\to\\script.ps1 '$variable'< NUL");

        if (strpos($s, 'True') === false) {
        header('Location: /succes.html'); 
        }
        else{
        header('Location: /failed.html'); 
        }

【问题讨论】:

  • 你错过了exit();。您的脚本在您的第一个 header() 处死去,因为它感觉需要重定向,但由于未知结束而无法继续。您 include()require_once() 您的加载页面,然后在每个 header() 方法之后添加一个 exit();See how it should be here
  • 为什么你要先使用重定向,而且只有在你在 shell 脚本中调用重定向之后才使用重定向?据我所知,这里发生的是: 1. 服务器将重定向标头发送到浏览器。 (代码中的第一行) 2. 浏览器请求 loading.html 3. 服务器向浏览器发送 loading.html 脚本的其余部分将保持未执行状态。

标签: php powershell redirect shell-exec


【解决方案1】:

在此构造中无法加载页面,因为您的页面仅在文件加载完成时才会显示。如果您通过 AJAX 请求运行包含/外壳程序,它会。下面的解决方案是索引页面(loading.html)对文件request.php运行AJAX请求并将其内容复制到loading.html的内容中。

loading.html

<!-- Your loading.html content --->

<script type="text/javascript" src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<script type="text/javascript">
$.post( "request.php", function( data ) {
  $( "body" ).html( data );
});
</script>

request.php

include ("file\where\the\variable\comes\from.php");
$s = shell_exec("powershell path\\to\\script.ps1 '$variable'< NUL");

if (strpos($s, 'True') === false) {
  echo file_get_contents('succes.html'); 
}
else {
  echo file_get_contents('failed.html'); 
}

【讨论】:

  • 谢谢,效果很好。唯一的问题是我的服务器还不能访问互联网,所以我只是将 jquery 脚本放在本地。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-07-21
  • 2011-09-13
  • 1970-01-01
  • 1970-01-01
  • 2012-10-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多