Execute a shell script from HTML page with PHP - 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.

Related

header() function does not redirect

i am facing a problem using the php header() function. I am trying to redirect to another page after form submission. The main thing is, that it works fine on my local XAMPP server, but not on my web server hosted by strato..
I have already searched the web for solutions but not a single one applies to my problem. So there is no whitespace before my php tag, i do not ouput anything before i call the header function and there is also nothing included that can cause any problems. I've also tried to use absolute pathes to state the page i would like to redirect to. No result..
Here is the whole content of my file redirect.php
<?php
ini_set ("display_errors", "1");
error_reporting(E_ALL);
if (isset($_POST["submit"])) {
// redirect
header("Location: register.php"); exit;
//echo "<script language='javascript'>window.location.href='register.php';</script>";
}
?>
<!doctype html>
<html>
<head>
<title>Redirect</title>
</head>
<body>
<form action="redirect.php" method="post">
<button type="submit" name="submit">Submit</button>
</form>
</body>
</html>
There is no error output on the page, instead it will stay completly blank after form submission..
The redirection via javascript is working, but is just a workaround since it will not work if javascript is disabled.. But it shows that the if-statement will be reached!
Any suggestions?

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.

Html form and php function on same file

I have a html form in register.html. When submitted, the data is inserted into a database, using the script from register.php.
Since, there are just a few line of code, I would like to put them both in the same file. Right now I have:
<form action="register.php" METHOD="POST">
If I put the form and the php script in the same file, what should I call on action=" " ?
Use
action="<?=$_SERVER['PHP_SELF']?>"
$_SERVER is a reserved variable -- see more here.
You can keep action the name of the file you're calling from. But in the top of the file, make sure to check if the form has been posted or not and then process it before you even get to the head of the page. It'll have to be a php page though, not html.
So it would look something like:
<?php
if (!empty($_POST['whatever']))
{
//process the database stuff here
}
?>
<html>
<head>
</head>
<body>
</body>
</html>
action takes the form of a relative or absolute pathing, it doesn't matter where the form is.
You'll need to user the action to the path of the file that catchs the POST. PHP doesn't care it came from the same file :)

Running .bat script from .php web page

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

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'];

Categories