T.Strever T.Strever - 3 days ago 7
PHP Question

Redirect then execute

I'm trying to go to the loading page and then execute the powershell script. But unfortunately I can't get this to work. When I go to the page it automaticaly goes to the script and redirects me to the succes of fail page.
I have tried to do a sleep after the redirect but it just waits few seconds and then runs the script and redirects me to the html page.

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

Answer

A loading page is not possible in this construction because your page only displays when your file is finished with loading. If you would run the include / shell through an AJAX request it would. The solution below is the index page (loading.html) that runs an AJAX request to the file request.php and copies his contents into the content of 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'); 
}
Comments