473,382 Members | 1,743 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

shutting down pc using PHP

34
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.
Expand|Select|Wrap|Line Numbers
  1. <script language="javascript">
  2.     import java.io.*;
  3.  
  4. public class shutdown
  5. {
  6. public static void main(String[] args) 
  7. {
  8. try {
  9. Runtime.getRuntime().exec("shutdown -s -t 0");
  10. }
  11. catch(IOException e) {
  12. System.err.println(e) ;
  13. }
  14.  
  15. }
  16. }
  17.     </script>
tks in advance!
Jul 29 '08 #1
6 12766
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($_time = 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($time, $force);
}
[/PHP]

HTML:
Expand|Select|Wrap|Line Numbers
  1. <form method="POST">
  2. Delay(seconds): <input type="text" name="time" value="30" /><br />
  3. Force shutdown?: <input type="checkbox" name="force" /><br />
  4. <input type="submit" value="shutdown" name="cmd">
  5. </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
Jul 29 '08 #2
Markus
6,050 Expert 4TB
This won't shut down a visitors PC, will it?

Exec() runs on the server, right?
Jul 29 '08 #3
pbmods
5,821 Expert 4TB
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).
Jul 29 '08 #4
Markus
6,050 Expert 4TB
(the thought of that being possible makes me feel all warm and fuzzy inside).
In a good way? Or a holycowivedranktoomuchandnowimgoingtobesick way?
Jul 29 '08 #5
Atli
5,058 Expert 4TB
In a good way? Or a holycowivedranktoomuchandnowimgoingtobesick 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.
Jul 29 '08 #6
mirainc
34
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($_time = 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($time, $force);
}
[/PHP]

HTML:
Expand|Select|Wrap|Line Numbers
  1. <form method="POST">
  2. Delay(seconds): <input type="text" name="time" value="30" /><br />
  3. Force shutdown?: <input type="checkbox" name="force" /><br />
  4. <input type="submit" value="shutdown" name="cmd">
  5. </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($_PCShutdown = 30, $_force = FALSE)
{
exec('shutdown -s -t ' . intval($_PCShutdown) . ($_force ? ' -f' : ''));
}
if (isset($_POST['powersav_update']) && ($_POST['powersav_update'] == 'Update'))
{
$time = $_POST['PCshutdown'];
shutDownPC($time*60);
}
?>
[/PHP]

Thanks again..
Jul 30 '08 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Till Plewe | last post by:
Is there a way to speed up killing python from within a python program? Sometimes shutting down takes more than 10 times as much time as the actual running of the program. The programs are...
6
by: EW | last post by:
I have a problem when using the python script found here: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/360649 It is a script to remotely shutdown a windows computer. When I use it,...
10
by: Jesper | last post by:
Does anyone know some c++ code for shutting down windows? I want to write a program using borland c++ builder to shut down windows. --
1
by: mikelostcause | last post by:
I'm working on shutting down an app that runs in the system tray, I have no problems shutting down, but I have problems saving data first. if the base.WndProc(ref m) is placed at the top, it...
4
by: Markus Stoeger | last post by:
Hi, I have a problem with Application.Run() when Windows is shutting down. Please have a look at the copy&paste example program below. The application has no forms. It has only got a notify...
7
by: Mark | last post by:
It is possible from a .NET application to prevent Windows from shutting down? I understand that a .NET application can "know" that windows is initiating the process of to shutting down - but...
3
by: leocwh | last post by:
Dear all, I would like to know how to run the execuatable before windows shutting down. Here is my simple code: Private Sub Command1_Click() Shell "C:\abc.bat", vbNormalFocus End Sub ...
2
by: Jack | last post by:
Sorry for the double post (also in the IIS group). We've got an ASP.Net 2.0 app running on IIS6. We kept losing sessions, and enabled health monitoring to see what was happening. This morning...
4
by: Larry Bud | last post by:
Not sure where else to post this.... One of our developers quit and I'm taking over stuff from him. He had a COM app that would lock files on occasion (yeah, thanks for leaving that in...
2
by: =?Utf-8?B?UGF1bA==?= | last post by:
Hi I am monitoring web apps with the windows event log and noticed several (Hosting environment is shutting down) statements. The server appears to still be running, just wondering any ideas on...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.