Is there a way for shutting down pc using PHP

半城伤御伤魂 提交于 2020-08-24 22:42:38

问题


I have an idea, and i have a single php local web page running on localhost

and i want to use GUI for 3 buttons : shut down , sleep and restart

Is PHP allows to me for these operations? Are there ready to write classes, methods in PHP?


回答1:


If your Apache/PHP configuration is setup to allow the use of system, then you can use that.

For *nix systems:

system('shutdown now');    // shutdown
// or
system('reboot');          // reboot
system('shutdown -r now'); // also reboot :)

For Windows:

system('shutdown -t -s 0'); // shutdown
// or
system('shutdown -r -t 0'); // reboot

More info here.

As far as sleep/standby on *nix is concerned, it varies a lot.


Note: You can also use shell_exec() or exec().


EDIT:

Per user1597430's comment, in *nix systems you can pass the -h (halt) option which will essentially shut down and halt the CPUs but not disconnect the main power.

system('shutdown -h now'); // shutdown and halt
system('shutdown -P now'); // shutdown and power off

Server fault has some great answers here about this.




回答2:


Almost all languages will have to do a system call.

shutdown -s -t 0

That is more or less the magic bullet. Java, for instance, could call it like this:

public static void main(String arg[]) throws IOException{
    Runtime rt = Runtime.getRuntime();
    Process proc = rt.exec("shutdown -s -t 0");
    System.exit(0);
}

PHP would be something like this

<?php
exec ('shutdown -s -t 0');
?>

But the thing is PHP is an interpreted language, compiled by a web-server that is signaled to run by an end user so it's probably way messy for that sort of behavior. It's like using a chainsaw to trim your nails...




回答3:


You can call shell script from php which will shut down , sleep and restart you computer it can be done on linux based machine, but not sure whether it is possible on Windows pc




回答4:


You can execute a .bat file with PHP on Windows.

<?php echo exec('reboot.bat'); ?>

Content of the .bat file:

shutdown example:

C:\Windows\system32\shutdown.exe -s -c "Shutdown the computer."

reboot example:

C:\Windows\system32\shutdown.exe -r -c "Rebooting for latest updates."

StandByModus

rundll32.exe powrprof.dll,SetSuspendState


来源:https://stackoverflow.com/questions/13538970/is-there-a-way-for-shutting-down-pc-using-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!