Running .bat script from .php web page - php

I am attempting to load a webpage on my own server which will run a .bat script (on the same server) as below.
When I access the page, called test.php, it display the 'DO IT!' button and when I press it, it just display the content on the .bat file rather than executing it on the server...
What do I need to configure on the server, I assume in the PHP settings, to force it to run the script rather than just display it on the webpage?
For the purpose of the question, I am happy about the security implications of what I am doing.
I am running a Windows machine with IIS and PHP.
<html>
<head>
<title>Restarting</title>
</head>
<body>
<?php
if(isset($_POST['submit']))
{
echo exec('c:\scripting.bat');
echo "Done!";
} else {
// display the form
?>
<form action="" method="post">
<input type="submit" name="submit" value="DO IT!">
</form>
<?php
}
?>
</body>
</html>

I think that the echo exec('c:\scripting.bat'); line it's causing you the problem. Try to just execute it without the echo statement.
If you trying to see the output of the function, you must use the second functions parameter: &$output, acording to the documentation itself. See it in the docs here.
I hope it will be useful to you! :D

Related

Execute a shell script from HTML page with PHP

I'm trying to execute a shell script from an HTML page using PHP. I have found a previous example here that I have been trying to follow but I'm having an issue. I'm not sure what the cause is but no errors are returned and no file is created from the bash script.
index.php:
<!DOCTYPE html>
<html>
<head>
<style></style>
</head>
<body>
<form action="./test.php">
<input type="submit" value="Open Script">
</form>
</body>
</html>
test.php
<?php
exec("./bash_script.sh");
header('Location: http://local.server.edu/ABC/abc_test/');
?>
bash_script.sh
#!/bin/bash
touch ./test_file.txt
One thing I have noticed that may be the cause of the issue is that it seems the path on the local server doesn't match with the file system exactly.
If I switch all the relative paths in the scripts to absolute paths such as:
/local/sequence/temp/abc_test/file.exe
Then after clicking the button to run the script I get an error saying:
The requested URL /local/sequence/temp/abc_test/test.php was not found on this server
Edit:
The three files They're located at /local/sequence/temp/abc_test And there is a symbolic link pointing to that directory at /export/www/htdocs/ABC
The error message seems to be indicating that test.php is not being found. As written, it needs to be in the same directory as index.php
You’ve tested the actual bash script, so we can proceed with the assumption that it’s in the execution of the script receiving the submission.
I would suggest putting all the web stuff into one page, because you can test sending and receiving input.
<?php
// for testing
// exec("./bash_script.sh");
// check for POST submission (this is not just reading data)
if(isset($_POST['runScript'])) {
// die('Request received');
exec("./bash_script.sh");
// It’s always proper to redirect after post :)
header('Location: http://local.server.edu/ABC/abc_test/');
die;
}
// finished with logic; show form
?>
<!DOCTYPE html>
<html>
<head>
<style></style>
</head>
<body>
<form method="POST">
<input type="submit" name="runScript" value="Open Script">
</form>
</body>
</html>
Note that I added the name attribute to the submit button, and made the form use POST method while submitting to the calling page (no action means submit to yourself).
I’ve also left a few commented actions to aid in debugging if necessary
You may have to adjust the path to the bash script. Currently it’s going to look in the same directory as index.php, which is not something you’d want to do in production.
You will be able to do that somehow, but its always very risky to allow such operations to execute from the php page.

PHP variables not showing up after submitting form (closed)

I'm just starting to learn form and PHP. I am testing a simple HTML file from W3Schools here with the following code:
<html>
<body>
<form action="welcome.php" method="get">
Name: <input type="text" name="name"><br>
E-mail: <input type="text" name="email"><br>
<input type="submit">
</form>
</body>
</html>
It is supposed to pass the information to a PHP file called welcome.php, which looks like this:
<html>
<body>
Welcome <?php echo $_GET["name"]; ?><br>
Your email address is: <?php echo $_GET["email"]; ?><br>
Random thing: <?php $rand = "bananas"; echo "$rand"; ?>
</body>
</html>
When I run the HTML file on Chrome, fill in the name and the email and press submit, the page looks like this:
Welcome
Your email address is:
Random thing:
While the HTML part works, the name variable, email variable and rand variable isn't printed.
[EDIT]: I solved it by transferring the files to a server and running it by going onto the actual webpage and it worked. Also Azeez Kallayi suggested running it on Xamp.
I think you are not using any server, just opening in broswer without any server. Also correct semicolon as in the above comment.
Since PHP is a server side programming language , you need a server to execute the PHP scripts.
There are many applications available that you can use as local servers and run your application. Some of them are below.
Wamp, Xamp, Lamp
Hope this will help you
Change this
echo "$rand";
To this
echo $rand;
Firstly you should get knowledge what PHP is and how you can use it.
PHP is a server-side scripting language. So if you try to run it like a html file you will not see the expected output. You need to understand what is a server side server-side scripting is.
If you have have jumped into coding is very essential you should know how you should debug to resolve your error.
One easy way is to enable error reporting.
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
?>
Hope this will give you a start .

HTML form button to run PHP to execute Python script

I am building an HTML document that is meant to run locally. On it is a button that I would like to have run a Python script when clicked. I'm trying to use a PHP-generated button. There's no input or output that I want to associate with the button; I just want it to run the script, which produces charts as image files that the rest of the page uses. I have tried a couple different ways to do it, including putting the PHP part in the HTML document, before the HTML:
<?php
if (isset($_POST['update']))
{
exec('python myScript.py');
}
>
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<form name="update" method="post" >
<button name = "update" type="submit"> Update charts </button>
</form>
</body>
I've also tried making the PHP code its own .php document in the same directory and calling it from the HTML code as so:
<form action = "updateCharts.php" method="post">
<input type="submit" name="update" />
</form>
where updateCharts.php is:
<?php
system('cd C:\My\Script\Path');
system('python MyScript.py');
?>
I've also tried substituting "system" with "exec" but to no avail. In the first part, I click the button and nothing happens. In the second, I click the button and I am taken to the text of the PHP document. In neither case does my Python script run!
Does anyone see what I'm missing? I'm admittedly a novice with php so it may be something glaring. Thanks for the help!
What this:
$command = escapeshellcmd('python /My/Script/Path/myscript.py');
// or
// $command = escapeshellcmd('/My/Script/Path/test.py');
// But you have to make your script executable doing: chmod +x myscript.py
$output = shell_exec($command);
echo $output;
This can not work.
<?php
system('cd C:\My\Script\Path');
system('python MyScript.py');
?>
You can not have a "session" over multiple system calls. Every system call has its own shell environment.
At least on Linux systems you can cascade commands by semicolons. If you need a work directory to be set before script execution, you could either do
system('cd C:\My\Script\Path ; MyScript.py');
or
chdir('C:\My\Script\Path');
system('MyScript.py');
However, script execution from PHP can be blocked, or apache might not have appropriate file permissions to the script, or, or, or.
You need to check log files and also post what actually is output.

PHP - Submit button does not work

I just started to learn PHP. I use a "Missing Manual" series book. I downloaded PHP 5, I installed it with the option "Do not setup a web server". From command line I can launch a php test program. But I can launch from browser.
The HTML code :
<html>
<head></head>
<body>
<h1>Welcome!</h1>
<form action="scripts/sayHelloWeb.php" method="POST">
<p><i>Enter your name:</i>
<input type="text" name="name" size="20" /></p>
<p><input type="submit" value="Say Hello" /></p>
</form>
</body>
</html>
and sayHelloWeb.php code is :
<html>
<head></head>
<body>
<h1>Hello, <?php echo $_REQUEST['name']; ?></h1>
</form>
</body>
</html>
Well, the HTML works : the text input and the buttons are displayed
, but php does not display any name. That is the "name" variable is empty. It displays only : "Hello, ". The folder scripts exists, the path is correct.
Where I did wrong ?
Thank you.
You have to setup a web server otherwise you can't run php file. if you check the source of sayHelloWeb.php after loading it, you will see that the php code is commented which means that it's not runned.
Make sure to install a web server. Wampserver is a good choise for beginners.
PHP is a Hypertext Preprocessor meaning that its not meant to be be interpreted on the client but rather on the server end, prior to the delivery of the HTML document itself. Installing a PHP interpreter along with a capable web server is required to make PHP work.
If you are seeing PHP code in your browser you can be sure that PHP is not set up correctly with your web server. Installing PHP itself merely installs a local PHP interpreter without setting it up to handle incoming web requests, for that you need a "proper" web server like Apache or IIS.
A good way to test if PHP is working is by creating this document:
<?php
phpinfo();
and loading it into your browser. Upon successful installation of PHP, a detailed (extremely verbose) status of PHP and web server components can be seen.
First check if is submit btn clicket. Than get value from field. Whitout that u will get notice
<?php
if ($_POST['submit']) {
$msg = $_POST['name'];
}
?>
<html>
<head></head>
<body>
<h1>Hello, <?php echo $msg ?></h1>
</form>
</body>
</html>
Try to use isset() and empty() on $_REQUEST
if (isset($_REQUEST['param']))
{
if(empty($_REQUEST['param']))
{
///
}
}
or
if ((isset($_REQUEST['submit'])) && (!empty($_REQUEST['name'])))
{
$name= $_REQUEST['name'];
}
<html>
<body>
<h1>Hello, <?php echo $_POST['name']; ?></h1>
</body>
</html>
Should work?
These fields are stored in $_POST array.
Try this: echo $_POST['name'];

PHP server event handling

I am looking for a way to have like a button in a web page, only when you press it the event handling is a program in a server. Like a trigger to execute a program in the server.
Is there any way to do this?
A PHP script can execute a command on the server using one of the various functions like exec, system, and passthru. It can be a security nightmare, so be careful.
You might want to look into something like: http://php.net/manual/en/book.exec.php
AJAX for the client part (sending notification about the button being pressed), and PHP (function exec() ou system() ).
Like the others have said, exec, system, and passthru can be used for that, but I believe you will need to disable safe mode.
safe_mode = Off
http://php.net/manual/en/features.safe-mode.php
To add on to this answer, you can make a form
Page1.html
<html>
<head>
</head>
<body>
<form action="script.php" method="post">
<input type="submit" />
</form>
</body>
</html>
script.php
<?php
shell_exec("cmd");
echo "done";
?>
Someone correct me if I am wrong here.

Categories