0

I have this script to start a bat file, and it does but after it starts it just crashes the web page, I need the script to redirect the user to a done page after the .bat has started...

<?php

exec('c:\WINDOWS\system32\cmd.exe /c START C:\Users\Administrator\Desktop\VGBR\steamapps\common\GarrysModDS\Start.bat');

?>

<?php
$var="Start";
echo "Starting...";
echo $var;
header("Location: done.php");
exit;
?>

This is what I've got so far...

Mark
  • 3,609
  • 1
  • 22
  • 33
Jonah Hart
  • 35
  • 5

1 Answers1

0

The reason is that when you use echo function PHP HAVE to send headers to the client, so any subsequent header will NOT be sent.

To correct that either you remove the echoes (as client won't see them) or you remove the header() and add a JavaScript that does the redirect.

Edit:
As Dave Chen stated, you shouldn't have that whitespace between the script outside the <?PHP tag as it is sent to the client (again, with headers).

To do what you are asking, do the script that way :

<?php
exec('c:\WINDOWS\system32\cmd.exe /c START C:\Users\Administrator\Desktop\VGBR\steamapps\common\GarrysModDS\Start.bat > NUL');

header('Location: /done.php');
die();
?>

EDIT 2
If the PHP keeps hanging, it is because it is waiting that .bat app to return something. In that case, check the following StackOverflow questions:
php exec command (or similar) to not wait for result
Running a PHP "exec()" in the background on Windows?

Community
  • 1
  • 1
jgabriel
  • 555
  • 2
  • 12
  • I did that, it opens it but indefinitely stays open in a tab... I need the tab to redirect after its done – Jonah Hart Jan 12 '14 at 00:11
  • I editted the code. Try it now. If it doesn't works, it probably means that the exec keeps running and php is waiting for it to return something before continuing – jgabriel Jan 12 '14 at 00:16
  • In that case, check this answer : http://stackoverflow.com/questions/3819398/php-exec-command-or-similar-to-not-wait-for-result – jgabriel Jan 12 '14 at 00:17
  • How do I do thhat? I'm new to PHP – Jonah Hart Jan 12 '14 at 00:37
  • http://stackoverflow.com/questions/7692263/running-a-php-exec-in-the-background-on-windows says `exec('c:\WINDOWS\system32\cmd.exe /c START C:\Users\Administrator\Desktop\VGBR\steamapps\common\GarrysModDS\Start.bat > NUL');` – jgabriel Jan 12 '14 at 00:46
  • Editted my answer with new link and changed code. Try and tell me if it works – jgabriel Jan 12 '14 at 00:47
  • Try `$WshShell = new COM("WScript.Shell"); $oExec = $WshShell->Run('c:\WINDOWS\system32\cmd.exe /c START C:\Users\Administrator\Desktop\VGBR\steamapps\common\GarrysModDS\Start.bat', 0, false);` – jgabriel Jan 12 '14 at 00:53
  • Fatal error: Class 'COM' not found in C:\xampp\htdocs\Access\repository\HT2\WH\www.voltzgaming.com\public_html\Admin\Server\Start\VGBR\Start.php on line 17 – Jonah Hart Jan 12 '14 at 00:55
  • I don't really know what your .bat file does nor your system specifications, that makes it a bit hard. Can you add an `echo` on your `.bat` ? P.S: that COM error means that your PHP doesn't support the windows COM – jgabriel Jan 12 '14 at 00:58
  • xampp with windows server 2008 – Jonah Hart Jan 12 '14 at 00:58
  • the cmd is a scrds console – Jonah Hart Jan 12 '14 at 00:59
  • Try the popen approach `pclose(popen('start /B C:\Users\Administrator\Desktop\VGBR\steamapps\common\GarrysModDS\Start.bat', 'r'));` – jgabriel Jan 12 '14 at 01:02
  • ( use the above code instead of the `exec()`. Tell me if it works ) – jgabriel Jan 12 '14 at 01:02