hakki
hakki

Reputation: 6517

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?

Upvotes: 4

Views: 20458

Answers (4)

Yes Barry
Yes Barry

Reputation: 9876

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.

Upvotes: 12

Roman Pantring
Roman Pantring

Reputation: 1

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

Upvotes: 0

Frankie
Frankie

Reputation: 25165

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...

Upvotes: 4

Pankaj Khairnar
Pankaj Khairnar

Reputation: 3118

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

Upvotes: 0

Related Questions