8

Before everyone starts butting in with "security risks" "cant be done" stop there and read the ENTIRE post

I have a web server set up from a home laptop which is serving as a games web server im trying to create a GUI so its easier for us to maintain the server and im trying to use batch files to do the actions on the computer

So to put this into perspective I have my index file index.php

<form method="post">
  <input type="submit" name="startServer" value="Start Server">
</form>

<?
  if(isset($_POST['startServer'])){
    exec('batch/startServer.bat');
  }
?>

And my startServer.bat will run on the laptop running the server and will do all the actions nesscary to start our game server so there is another directory "Instance" containing an excutable "Server.exe" which the batch file will run

The issue im having is running the web server and testing this it doesnt work if I open the batch file directly it works but it seems the php code doesnt work

For clarification I am using apache and my browser is chrome

And just a quick question for anyone willing to answer the route im going is correct right? Using php would allow everything to run on the machine hosting the server so the end user will only see the GUI and the server would run the batch files and everything on the web server and not the local machine if that makes sense?

EDIT: To be more clear about what's going on the function exec runs but it just hangs like the application is loading I need a solution that will actually open the application are my host computer for example if I wanted to open up notepad I press a button on the Web server and notepad will open on the computer

EDIT 2: I would like to note that I dont exactly need to use the exec function and I have tried all the answers to date 7/19/2017:3:45pm none are working if I do something on the sorts echo exec('start text.bat'); I will get a This is a test to show your batch is working and simply just have echo ..... in the batch file the main issue I am having is the server is not physically showing the opened file like displaying the GUI lets just take notepad for example

I can open notepad and get some return value as long as my batch file closes notepad once its finished running however the GUI for notepad is never displayed and thats very important

I read in a few articles about using apache as a service which im pretty sure I am but I do know that xaammp has suffiecient priveleges and I have checked the box that says "Allow apache to interact with desktop" however no GUI is popping up thats the main point I guess im trying to get across is I need to display the GUI not just open the file as a background service.

If it makes answering easier I am open to switching programming languages if theres one that can do what I want easier

FlamingGenius
  • 216
  • 3
  • 22
  • yep ive even tried putting the batch file in the same folder – FlamingGenius Jul 06 '17 at 07:22
  • @AlivetoDie You're not correct. PHP has a shorttag function. Its possible to only write `` and it works perfectly, as long as short tags are enabled in the `php.ini` file. Setting in php.ini: `short_open_tag=On` – Twinfriends Jul 06 '17 at 07:23
  • its php 7 u dont have to specify it – FlamingGenius Jul 06 '17 at 07:23
  • PHP does not need any special privilege to run .bat files. The key point is: what privileges are required by whatever other commands the .bat contains? – Álvaro González Jul 12 '17 at 16:20
  • A couple of things... you say you want it to run on the server, yet you expect an editor to open on your client (opening notepad would be a client thing and not a server side thing). Also if your 'startServer' required any sort of interaction, then this isn't going to work as you can't interact with a batch file (the way your running it anyway). – Nigel Ren Jul 17 '17 at 14:48
  • what is the content of your batch file? you might want to call `server.exe` with its full path or register it in your system path environment variable. and if you are running your php with apache as fast cgi, you also want to register the system path in apache [like this link](https://stackoverflow.com/questions/34723863/how-set-an-environment-variable-in-php-with-apache-fastcgi) – am05mhz Jul 18 '17 at 06:40

8 Answers8

3

Your theory is correct, it will run on the server however you may have issues running applications directly from php (with this method afaik it does not detach from the PHP, and the webapp "hangs" while the application is running).

Make sure: return values are printed / logged. Just an

<?php
  if(isset($_POST['startServer'])){
   echo exec('batch/startServer.bat');
  }
?>

Could point you to the right direction. The exec function may have been disabled in your distribution.

Using

<?php

instead of

<? 

is highly advised, by default short_tags are not enabled in most distributions (wamp, xamp, etc).

Set debug mode and print everything to get information about the problem:

<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

if(isset($_POST['startServer'])){
   echo exec('batch/startServer.bat');
}
?>

If you don't have any response, try a simple batch file with a "hello world" to test if it works.

Be aware, the rights and limitations are comes from the php environment, the batch file inherits the same rights running the PHP code / Apache (in case of mod_php)

Fiber
  • 96
  • 1
  • 4
  • nothing is wrong with the exec function as ive learned it seems i cant just "handoff" the assignment the page just constantly loads while the file is "opened" it can read text files and return in seconds at the most but still nothing is being opened on my computer I need these files to actually open physically – FlamingGenius Jul 06 '17 at 07:57
  • Until the batch file is finished, the exec won't return -> the page is going to act like still "loading". It won't open a console on your screen unless you start with something like this: https://stackoverflow.com/questions/17957076/keep-cmd-open-after-bat-file-executes – Fiber Jul 06 '17 at 08:04
  • is there a better language I can use that could accomplish this easier? – FlamingGenius Jul 06 '17 at 08:11
  • I don't think its simply a language choice, the problem is much more complex than a simple language change could solve. Of course maybe there are softwares made for this task, but its not too common (never seen anything like this). Maybe the game server has an admin webserver? – Fiber Jul 06 '17 at 08:12
  • Indeed it is! ive tried just about everything i can think of man and i thought setting up port forwarding was hard! – FlamingGenius Jul 06 '17 at 08:15
  • Just found something what might be exactly a solution to your problem: https://pterodactyl.io/ – Fiber Jul 17 '17 at 10:09
2

In php manual about exec function, there is a note :

Note: If a program is started with this function, in order for it to continue running in the background, the output of the program must be redirected to a file or another output stream. Failing to do so will cause PHP to hang until the execution of the program ends.

I think "hangs like the application is loading" is your application waiting for the bat file terminated / closed to get the output result

Let's try another approach, i found it here, the concept is to create a scheduler that execute the program you want and call it using command.

hope this help :

shell_exec('SCHTASKS /F /Create /TN _notepad /TR "notepad.exe" /SC DAILY /RU INTERACTIVE');
shell_exec('SCHTASKS /RUN /TN "_notepad"');
shell_exec('SCHTASKS /DELETE /TN "_notepad" /F');

If this doesn't work

Check whether you have declared safe_mode = Off inside php.ini

Anjana Silva
  • 8,353
  • 4
  • 51
  • 54
Baim Wrong
  • 478
  • 1
  • 10
  • 14
  • Redirection of output from a batch script in windows: https://stackoverflow.com/questions/9384044/redirect-output-from-a-command-within-a-batch-file-whose-output-is-already-redir – psx Jul 19 '17 at 14:53
0

From here:

How do you run a .bat file from PHP?

Have you tried: system("cmd /c C:[path to file]"); ?

bones
  • 808
  • 3
  • 10
  • 23
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/low-quality-posts/16703552) – miken32 Jul 13 '17 at 23:58
  • @miken32 I did include the answer – bones Jul 14 '17 at 01:46
  • If the answer is over there then flag this as a duplicate. – Blastfurnace Jul 14 '17 at 06:02
0

You might need to run it via cmd, eg:

system("cmd /c C:[path to file]");

Or Try following options

1.

<?php
exec('c:\WINDOWS\system32\cmd.exe /c START C:\Program Files\VideoLAN\VLC\vlc.bat');
?>

2.

When you use the exec() function, it is as though you have a cmd terminal open and are typing commands straight to it.

Use single quotes like this $str = exec('start /B Path\to\batch.bat'); The /B means the bat will be executed in the background so the rest of the php will continue after running that line, as opposed to $str = exec('start /B /C command', $result); where command is executed and then result is stored for later use.

<?php
 pclose(popen("start /B test.bat", "r")); die();
?>
Siddhesh
  • 1,095
  • 1
  • 11
  • 23
0

i think this is a containment issue.

if you run the app under the process of php run by iiswebuser when php terminates it will close all spawned child processes in windows. there is a very quick way a command to break an application out of the child process containment using the start command.

if(isset($_POST['startServer'])){
    exec('start batch/startServer.bat');
}  

Diagram of containment as i explained it (simplisticly)

  • IIS (IIS runs as an IISUser)
    • php (application)
      • cmd.exe (batch)

using start bring it to the root of that tree

  • IIS (IIS runs as an IISUser)
    • php (application)
  • cmd.exe (batch)
Barkermn01
  • 6,781
  • 33
  • 83
0

Baim Wrong was correct in the first part of the response: you have to redirect output of the script or your PHP code will hang. Also, you have to move process in the background.

This is easy to do on *nix:

system("/usr/local/bin/shell.sh >> /tmp/log.log 2>&1 &");

I know that you can redirect the output on Windows but not sure how to move the process in the background. You should check DOS manual or try with power shell.

  • Yes. For completeness - redirection of output from a batch script in windows: https://stackoverflow.com/questions/9384044/redirect-output-from-a-command-within-a-batch-file-whose-output-is-already-redir – psx Jul 19 '17 at 14:53
0

you can use either system or exec php function

$path = __DIR__ . '/batch/startServer.bat';
exec('cmd /c start ' . $path);

or

$path = __DIR__ . '/batch/startServer.bat';
$lastLine = system('cmd /c start ' . $path);
Ali Faris
  • 17,754
  • 10
  • 45
  • 70
0

You are having some issue about running application directly from exec. I was having the same issue of running file using exec. It was solved by passing another parameter 2>&1.

exec('some_command 2>&1', $output);
print_r($output);  // to see the response to your command

Check the values printed by output see exec function

<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
$output = array();
if(isset($_POST['startServer'])){
   exec('batch/startServer.bat 2>&1', $output);
   print_r($output);
} else {
    echo "Not posted";
}
?>
Shahrzad
  • 1,062
  • 8
  • 26