简体   繁体   中英

Does PHP CLI on macOS and Linux using the built-in Web server behave like it does on Windows for executing commands?

This is working on my Windows 10 system, but I'm very skeptical as to whether it will also work on Linux and macOS. I want to ensure that it does, because it's incredibly useful to be able to run commands to open various applications from the localhost "web control panel". I do it all the time, but don't have a Mac or a Linux PC for testing.

Steps to reproduce:

  1. Make sure that you're running the built-in PHP webserver and not something else.

  2. Create a file called test.php with this content and put it in your WWW root:

     if (isset($_GET['run'])) shell_exec('winver'); echo '<p><a href="?run=1">test me</a></p>';

Obviously change the "winver" part to a command that macOS or Linux will understand. As long as it brings up some kind of GUI window when run.

  1. Go to http://127.0.0.1/test.php and click the hyperlink.
  2. It should now run the specified command and thus display a GUI box of some kind on the screen.

Does this happen on macOS and Linux, or is this a "lucky coincidence" that just happens to work on Windows but wasn't intended?

I've just tested this and successfully launched Microsoft Word on macOS and xcalc on Linux.

You do have to be careful that the user the web server is running has has access to the desktop (eg in Linux you need to have DISPLAY set correctly etc) but that isn't an issue if you are using php -S on the same desktop you want to display the GUI application on.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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