Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way for shutting down pc using PHP

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?

like image 887
hakkikonu Avatar asked Dec 05 '22 13:12

hakkikonu


1 Answers

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.

like image 61
Yes Barry Avatar answered Dec 07 '22 03:12

Yes Barry