Using HTML with PHP and shell exec to search for files - php

I am a complete beginner in PHP.
I am trying to create a HTML/ PHP Script that will use the user input from an HTML form and than use php shell_exec to search for files with that input using the "find /var/www -Name " command.
I know how ro run a simple script with PHP, but I have no idea how do do that with user Input...
Eg: "
<?php
if (isset($_POST['button']))
{
exec('test.sh');
}
?>
<html>
<body>
<form method="post">
<p>
<button name="button">Run Script</button>
</p>
</form>
</body>
"
This is how far I've got. I've decided two make to files, one HTML, the second one the PHP script:
search.html
<html>
<body>
<form action="search.php" method="post">
keyword: <input type="text" name="keyword"><br>
<input type="submit">
</form>
</body>
</html>
php script:
search.php
<html>
<body>
<?php shell_exec('find /var/www -Name "keyword"') $_POST["keyword"]; ?>
</body>
</html>

This should do the trick :
<?php
$keywords=$_POST["keyword"];
$result=shell_exec('find /var/www -Name "'.$keywords.'"');
echo '<pre>'.$result.'</pre>';
?>
BEWARE: it is a bad idea to use such a command, because you are using user input directly, user can run any kind of command on the server.
For example, if a user type "; rm -rf /var/www;echo " as search, it will delete the whole content of your /var/www folder.
You'd better implement a php function that will do the same thing as your find command.
However, you MUST ALWAYS do sanitize any user input, everything that comes from the outside world is evil..

Got it:
<?php
$keyword=$_POST["keyword"];
$result=shell_exec(' find /var/www -name '.$keyword.'');
if (!isset($_POST['submit'])) { // if page is not submitted to itself echo the form
?>
<html>
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css">
</head>
<body>
<div class="container">
<div class="jumbotron">
<h1>Search</h1>
</div>
<div class="row">
<div class="col-md-4">
<title>Search</title>
</head>
<body>
<form method="post" action="<?php echo $PHP_SELF;?>">
Suchen:<input type="text" size="12" maxlength="500" name="keyword"><br />
<input type="submit" value="submit" name="submit">
</form>
<?
} else {
echo " ".$result."<br />";

Related

How to get php variable from an action form and use it in the form thats activating the action

I have two files here as a test the first one which is this below and when I click submit it suppose to do the action on the next page but I want to know how to get retrieve athe $life variable from the action php file and put it in the normal html file
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<form class="" action="../logic/profileAction.php" method="post">
<label for=""></label>
<button type="submit" name="button">Submit</button>
</form>
</body>
</html>
Second file which is the php file:
<?php
$life ="Yo";
?>
check this code. you need to run this code in server
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<?php
include_once 'edit.php';
echo $life;
?>
<form class="" action="../logic/profileAction.php" method="post">
<label for=""></label>
<button type="submit" name="button">Submit</button>
</form>
</body>
</html>
and this is your include edit.php
<?php
$life = 'Ok';
?>
then your first file show ok when you run this code

$_POST is not returning anything

I am new in using PHP - i am trying to get the data from data sent to the Apache server using $_POST - but i am getting nothing
below is the details
i am using XAMPP on Windows 7 for setup (Apache & PHP)
and I am having two files
welcome.html which is calling welcome.php to echo the contents got from the html
Note that I have nothing reported in Apache error log file
C:\xampp\apache\logs\error.log
any idea what went wrong here
<head>
<meta charset="utf-8"/>
</head>
<body>
<form action="welcome.php" method="post">
Name: <input type="text" name="name"><br>
<input type="submit">
</form>
</body>
and
welcome.php
<?php
error_reporting(E_ALL);
?>
<head>
<meta content="text/html;charset=utf-8" http-equiv="Content-Type">
</head>
<body>
Welcome
<?php echo $_POST["name"]; ?><br>
</body>
for testing your php file. direct run this welcome.php from your localhost
like this http://localhost/welcome.php
<?php
echo 'Check your name';
?>
if you see "Check your name"; then your local server is working . else need to run local server
this is your html welcome.html file
<head>
<meta charset="utf-8"/>
</head>
<body>
<form action="welcome.php" method="post">
Name: <input type="text" name="name"><br>
<input type="submit">
</form>
then submit your form using name text
then check your code using
<?php
error_reporting(E_ALL);
print_r($_POST);
?>
<head>
<meta content="text/html;charset=utf-8" http-equiv="Content-Type">
</head>
Welcome
everything work here

Submitting jscolor result to shell_exec command

To start with: My web coding skills are nearly to non existent, I just did a bit of HTML ages ago...
And my first steps with php are just made today.
My goal is: Having a color picker and sending the picked value as an argument to an python script.
I managed to use the jscolor picker libary (http://jscolor.com/examples/) and I've been also able to run the python scripts with an (color) argument from php.
My problem is now:
How do I submit the color string (hex-str) to the exec command (LED_color)?
<?php
$LED_color="FFFFFF";
$LED_color=escapeshellarg($LED_color);
if (isset($_POST['button']))
{
$LED_color = $_REQUEST['hex-str'];
echo shell_exec("sudo /home/pi/LEDscripts/color-by-arg.py $LED_color");
}
?>
<html>
<head>
<title>Basic usage</title>
</head>
<body>
<div style="position:absolute; left:280px; top:10px;">
toString = <span id="hex-str"></span><br />
</div>
<script src="jscolor.js"></script>
Color: <input class="jscolor {closable:true,closeText:'Close me!',onFineChange:'update(this)'}" value="000000">
<script>
function update(picker) {
document.getElementById('hex-str').innerHTML = picker.toString();
}
</script>
<form method="post">
<p>
<button name="button">Submit color</button>
</p>
</form>
</body>
</html>
Your color picker input needs a name attribute to match your PHP code, and also needs to be inside the <form> element.
<?php
if (!empty($_POST["hex-str"])) {
$LED_color = escapeshellarg($_POST["hex-str"]);
echo shell_exec("sudo /home/pi/LEDscripts/color-by-arg.py $LED_color");
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Basic usage</title>
<script src="jscolor.js"></script>
</head>
<body>
<form method="post">
<p>
Color:
<input type="text" name="hex-str" value="000000" class="jscolor" data-jscolor="{closable:true,closeText:'Close me!',onFineChange:'update(this)'}"/>
toString = <span id="hex-str-display"></span>
</p>
<p>
<button type="submit">Submit color</button>
</p>
</form>
<script>
function update(picker) {
document.getElementById('hex-str-display').innerHTML = picker.toString();
}
</script>
</body>
</html>

Php file not displaying data collected from html file

I'm trying to link a simple html file with a php file.The data from the html file is properly getting transferred to php file , but while displaying that data through php file nothing is gets displayed on the browser.
html:
<!DOCTYPE html>
<html>
<head>
<title>Calculator</title>
</head>
<body align="center">
<header align ="center"><i><b><font size="30">Calculator</font></b></i></header>
<form action="result.php" method="POST" >
<br>
<br>
Number 1: <input type="text" name="number1">
<br><br>
Number 2: <input type="text" name="number2">
<br>
<br>
<input type="submit">
</form>
</body>
</html>
php:
<!DOCTYPE html>
<html>
<head>
<title>Calculator</title>
</head>
<body align="center">
<header align ="center"><i><b><font size="30">RESULT</font></b></i></header>
<?
$var1= $_POST['number1'];
$var2= $_POST['number2'];
echo $var1;
echo $var2;
?>
</body>
</html>
<form action="result.php" method="GET" >
you are using GET and receiving as POST
$var1= $_POST['number1'];
$var2= $_POST['number2'];
you change either one of them, but my advice would be to change your form action to:
<form action="result.php" method="POST" >
You're are using method as GET in your source file but getting as $_POST in the destination file.
You should use method="POST" from your source file.
So, It should have something like
<form action="result.php" method="POST" >
Learn more about Sending data from Form Here
How do you know that the data is beign transfered? Try this as it seems that you are using GET instead of post, then use the proper one:
<?
echo 'POST: ';
var_dump($_POST);
echo 'GET: ';
var_dump($_GET);
?>
As of this:
<form action="result.php" method="GET" >
you should either do:
$var1= $_GET['number1'];
$var2= $_GET['number2'];
or change
<form action="result.php" method="POST" >
(which is anyway recommended ...)
<!DOCTYPE html>
<html>
<head>
<title>Calculator</title>
</head>
<body align="center">
<header align ="center"><i><b><font size="30">RESULT</font></b></i></header>
<?php
$var1= $_GET['number1'];
$var2= $_GET['number2'];
echo $var1;
echo $var2;
?>
</body>
</html>

exec command not working (php)

I am working on a php app and this is my code:
<html>
<head>
<meta charset="UTF-8" />
</head>
<?php
if (isset($_POST['PLAY']))
{
exec("open /Applications/Chess.app");
}
?>
<form method="post">
<button name="PLAY">Play Chess</button><br>
</form>
</html>
But when I run it, and press the button the Application does not open. Thanks for any help!
There is an space before /Applications/Chess.app
try
exec("open/Applications/Chess.app");

Categories