PHP form shell_exec output - php

I am trying to do a small project (for personal use) and I am a little stuck with a particular part of this.
I have the following:
<html>
<head>
</head>
<body>
<form action="" method=post>
<input type="text" name="ipaddress"/>
<input name="submit" type=submit value="Command">
</form>
<?php
if(isset($_REQUEST['submit'])) {
$ipaddress = $_REQUEST['ipaddress'];
$output = shell_exec("ping -c 5 $ipaddress");
echo "<pre>$output</pre>\n";
}
?>
</body>
</html>
This is basically a test box in a form where a user can enter an IP address. This does work, and outputs the results on the page. What I'm trying to achieve is a live output of the response coming through as if this was being run from the CLI (essentially a line by line return of the output).
I have looked at various methods, including SSE but unfortunately, I've been unable to achieve this.
Can anyone please point me in the right direction or let me know where I'm going wrong?
Thanks

Related

basic calculator php error redirect to another page after clicking submit

I'm a php starter, and i don't know what's wrong with my code, it's supposed to be a simple basic calculator, first i started a server with the command php -S localhost:4000 succesfully, i've been trying lots of code examples and they work fine, but this one doesn't:
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<form action="index.php" method="get">
Number 01: <input type="number" name="num1"><br>
Operation: <input type="text" name="op"><br>
Number 02: <input type="number" name="num2"><br>
<input type="submit"><br>
</form>
<?php
$num1=$_GET['num1'];
$num2=$_GET['num2'];
$result="";
$op=$_GET["op"];
if ($op=='+') {$result=$num1+$num2;}
if ($op=='-') {$result=$num1-$num2;}
if ($op=='*') {$result=$num1*$num2;}
if ($op=='/') {$result=$num1/$num2;}
echo "Result = $result";
?>
</body>
</html>
and the error i receive whenever i click submit query, another page shows up:
it says; ping service in the title, and a little frame appears:
Blocked by Content Security Policy
An error occurred during a connection to www.root-me.org.
Firefox prevented this page from loading in this way because the page
has a content security policy that disallows it.
I tried the code in an online php editor and same problem, is there something terribly wrong in my code, please help me! thank you
Problem Fixed
i changed the url from http://localhost:4000/site/ to http://localhost:4000/site/index.php
so the problem was actually in the form action attribute for he didn't recognize the index.php file!
Thank you

What program is used to process php files within an html context?

I'm just following w3schools like a normal person
For me getting php to work is harder than learning the language itself...
If somebody could please provide an extensive guide on common problems such as this one that would be heavily resourceful
I'd also like to know the ins and outs of running php scripts within an html context, not having the php extension on the browser would be preferred in most cases
I can't get the following script to run upon submitting the form. It ends up opening up a window asking me which program should be used to run the php file.
<!DOCTYPE html>
<html>
<head>
</head>
<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">
</body>
</form>
Opening the php file using firefox accomplishes the same thing.
If so, which program should I use? or do I need to update some configuration
in order to get this simple script to work
<html>
<body>
Welcome <?php echo $_POST["name"]; ?><br>
Your email address is: <?php echo $_POST["email"]; ?>
</body>
</html>
One of the easiest ways to get PHP up and running for a newbie would be to use XAMPP. See this Wikihow article on how to get that done: https://www.wikihow.com/Set-up-a-Personal-Web-Server-with-XAMPP
Good luck!

No echo for form data using GET method in PHP

Fairly new to PHP. Have been trying a simple program to get data from 2 fields and display it using GET method.
Heres the HTML code
<html>
<head>
<title>Simple Form</title>
</head>
<body>
<form action="send.php" method="get" id="sample">
Name:<input type="text" name="user"/>
Message:<input type="text" name="message"/>
<input type="submit"/>
</form>
</body>
</html>
Heres the PHP code:
<html>
<body>
Welcome <?php echo $_GET["user"]; ?>
Message <?php echo $_GET["message"]; ?>
</body>
</html>
The GET method fetches the data as a QueryString in address bar of browser, but doesn't display anything in browser window.
The code works perfectly. Reinstalling WAMP server solved the issue.
You need to start the server.
Go to your directory where the index.html file of your website lives, then run the command line php -S localhost:5000.
5000 is the port where php is listening to, you can put any value there....
Install php if you haven't.. Or Wamp

Calling external program with many arguments from website

I am a very new to web programming and perhaps this questions might seem to obvious. I have a form on the website and a button. When the button is pressed I want to call external program (Linux executable) which is located on the server, and pass all the text data from the filled form on the page to that program as arguments, and then get the output back to user. For ex: (./myprogram username userjob ...).
How can this be implemented? What language should I use? Javascript, PHP, Python?
Thank you
You can execute the program with the system call and add the arguments posted by the user to the end of it. It would look something like this:
$theResults = system(escapeshellcmd('./myProgram '.$_REQUEST['arguments']));
echo $theResult;
A full working example would be something like this:
<?php
if(!empty($_REQUEST['arguments'])){
$results = system(escapeshellcmd('./myProgram '.$_REQUEST['arguments']));
}
?>
<html>
<head>
<title></title>
</head>
<body>
<?php
if(!empty($results)){
echo $results;
}
?>
<form method="post" action="">
Your Arguments: <input type="text" name="arguments" value="" /><input type="submit" name="Submit" value="Submit" />
</form>
</body>
</html>
You can simply use php for that:
<?php exec("./yourscript.sh");

passing data from html to php

dear all
I am a beginer in PHP world (PHP 5.3.5) my web server is IIS fastCGI on win xp
I tried to pass values from the HTML form to the php but the data are not passed
this is my html file
<html>
<body>
<form action="welcome.php" method="post">
Name: <input type="text" name="fname" />
Age: <input type="text" name="age" />
<input type="submit" />
</form>
</body>
</html>
and this is my php file welcome.php
<html>
<body>
Welcome <?php echo $_POST["fname"]; ?>!<br />
You are <?php echo $_POST["age"]; ?> years old.
</body>
</html>
After pressing submit the output was like this
Welcome!
You are years old.
but it should be like this
Welcome John!
You are 28 years old.
Can you please help me with this.
on IIS change in the form: method="post" to method="POST" <-- UPPERCASE should solve your issue.
Anyway what about the apache? and PHP version around just 5, flat five :)
5.3.5 on IIS for the beginner? sounds like to get the Mount Everest by night without gloves
That is pretty standard simple code, and looks correct. One thing you might look at is the way PHP is configured on your server. Also, try using $_REQUEST in place of $_POST on your welcome.php page and see if it still does the same thing.
Try naming your form as well
After looking at your code it seems that it must be a configuration issue.
Steps to resolve this:
Firstly is the code that spits out the page actually welcome.php
Secondly add the following to your php block
var_dump($_POST);
Click submit and if this still shows
array (
)
if so then do
var_dump($_REQUEST);
and post the contents to your post, and show us.

Categories