shutting down pc using PHP

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mirainc
    New Member
    • Jul 2008
    • 34

    shutting down pc using PHP

    I'm doing a project on Energy saving in PCs. I'm doing a web application using PHP. I need to do a PC shutdown but the codes i've tried does not work. Anyone have a soultion. PC shutdown in WIndows 2003- vista. These are the code tat i found.
    [code=php]
    <script language="javas cript">
    import java.io.*;

    public class shutdown
    {
    public static void main(String[] args)
    {
    try {
    Runtime.getRunt ime().exec("shu tdown -s -t 0");
    }
    catch(IOExcepti on e) {
    System.err.prin tln(e) ;
    }

    }
    }
    </script>[/code]

    tks in advance!
    Last edited by pbmods; Jul 29 '08, 11:11 AM. Reason: Added CODE tags.
  • rohypnol
    New Member
    • Dec 2007
    • 54

    #2
    PHP[PHP]<?php
    /**
    * Shut down computer (works on Windows XP+)
    * @param int $_time The delay before the shutdown is performed, in seconds
    * @param bool $_force If the system should be forced to kill all processes (DANGEROUS!)
    */
    function shutDownPC($_ti me = 30, $_force = FALSE)
    {
    exec('shutdown -s -t ' . intval($_time) . ($_force ? ' -f' : ''));
    }

    if (isset($_POST['cmd']) && ($_POST['cmd'] == 'shutdown'))
    {
    if (isset($_POST['time'])) $time = $_POST['time'] else $time = 30;
    if (isset($_POST['force'])) $force = ($_POST['force'] == 'on') else $force = FALSE;
    shutDownPC($tim e, $force);
    }
    [/PHP]

    HTML:
    Code:
    <form method="POST">
    Delay(seconds): <input type="text" name="time" value="30" /><br />
    Force shutdown?: <input type="checkbox" name="force" /><br />
    <input type="submit" value="shutdown" name="cmd">
    </form>
    I haven't tested the code, but it should work :o)
    All you do is call exec() (executes a system command just like when you type it in the cmd console) and tell it to run the shutdown program which should exist on XP+ systems.

    For more information, see http://www.php.net/function.exec and http://www.ss64.com/nt/shutdown.html.

    Regards,
    Tom

    Comment

    • Markus
      Recognized Expert Expert
      • Jun 2007
      • 6092

      #3
      This won't shut down a visitors PC, will it?

      Exec() runs on the server, right?

      Comment

      • pbmods
        Recognized Expert Expert
        • Apr 2007
        • 5821

        #4
        Originally posted by markusn00b
        This won't shut down a visitors PC, will it?

        Exec() runs on the server, right?
        Correct. There is no way to shut down a client's PC over the web (the thought of that being possible makes me feel all warm and fuzzy inside).

        Comment

        • Markus
          Recognized Expert Expert
          • Jun 2007
          • 6092

          #5
          Originally posted by pbmods
          (the thought of that being possible makes me feel all warm and fuzzy inside).
          In a good way? Or a holycowivedrank toomuchandnowim goingtobesick way?

          Comment

          • Atli
            Recognized Expert Expert
            • Nov 2006
            • 5062

            #6
            Originally posted by markusn00b
            In a good way? Or a holycowivedrank toomuchandnowim goingtobesick way?
            Depends on how you felt about the 1980's, seeing as that is about as far we would be pushed back if websites would be able to gain that sort of control over client systems.

            Comment

            • mirainc
              New Member
              • Jul 2008
              • 34

              #7
              Originally posted by rohypnol
              PHP[PHP]<?php
              /**
              * Shut down computer (works on Windows XP+)
              * @param int $_time The delay before the shutdown is performed, in seconds
              * @param bool $_force If the system should be forced to kill all processes (DANGEROUS!)
              */
              function shutDownPC($_ti me = 30, $_force = FALSE)
              {
              exec('shutdown -s -t ' . intval($_time) . ($_force ? ' -f' : ''));
              }

              if (isset($_POST['cmd']) && ($_POST['cmd'] == 'shutdown'))
              {
              if (isset($_POST['time'])) $time = $_POST['time'] else $time = 30;
              if (isset($_POST['force'])) $force = ($_POST['force'] == 'on') else $force = FALSE;
              shutDownPC($tim e, $force);
              }
              [/PHP]

              HTML:
              Code:
              <form method="POST">
              Delay(seconds): <input type="text" name="time" value="30" /><br />
              Force shutdown?: <input type="checkbox" name="force" /><br />
              <input type="submit" value="shutdown" name="cmd">
              </form>
              I haven't tested the code, but it should work :o)
              All you do is call exec() (executes a system command just like when you type it in the cmd console) and tell it to run the shutdown program which should exist on XP+ systems.

              For more information, see http://www.php.net/function.exec and http://www.ss64.com/nt/shutdown.html.

              Regards,
              Tom
              Ok i tried your code but the page would not display.. so i adjusted it abit and managed to get the PC to shutdown. Thanks so much for your help..

              Here is the code that worked for me.. just incase others might want to try:


              [PHP]
              <?php
              function shutDownPC($_PC Shutdown = 30, $_force = FALSE)
              {
              exec('shutdown -s -t ' . intval($_PCShut down) . ($_force ? ' -f' : ''));
              }
              if (isset($_POST['powersav_updat e']) && ($_POST['powersav_updat e'] == 'Update'))
              {
              $time = $_POST['PCshutdown'];
              shutDownPC($tim e*60);
              }
              ?>
              [/PHP]

              Thanks again..

              Comment

              Working...