0

I have the following PHP post from HTML

The output from the mvsce -d 1234-56-78 -l 1 -s 1 command is displayed from bash as follows

TAR 332 
PCXLA-TAACC 

over the two lines.

The code below only ever outputs the 2nd line PCXLA-TAACC

<?php
$mvdate=$_POST['fdate'];
$mvlevel=$_POST['flevel'];
$mvsite=$_POST['fsite'];

$mvse = shell_exec ('/usr/local/bin/mvsce -d "'.$mvdate.'" -l "'.$mvlevel.'" -s "'.$mvsite.'"');

echo $mvse;

?>

How can I get the code to display both lines in a browser windows?


The HTML I use to post for the PHP

<!DOCTYPE html>
<html>
<head>
</head>
<body>
<H1>makeVSCE Output</H1><br>
<br>
<br>
<form name="myForm" action="makevsce.php"
method="post">
Date (YYYY-MM-DD):<br>
<input TYPE="text" NAME="fdate"><br>
Level:<br>
<input TYPE="level" NAME="flevel"><br>
Sequence:<br>
<input TYPE="Sequence" NAME="fsite">

<input type="submit" value="Submit">
</form>
<br>
</body>
</html>
5
  • Can you run the same command (minus any $_POST variables) as your not running the same thing (you have quotes in your shell_exec() call for example. Commented Mar 19, 2018 at 13:44
  • SO I have substitude the variables in the command to actual figures. running the php from command line reports both lines to console. Run the PHP from a browser and only the 2nd line is displayed Commented Mar 19, 2018 at 13:56
  • All I'm saying is try the exact command you tried from the command line - cut and paste it into your script. Commented Mar 19, 2018 at 13:57
  • I did that run the php from console it displays both lines as expected. running from a browser only the 2nd line is displayed. Commented Mar 19, 2018 at 14:10
  • Then it is an issue with the browser's page rendering. You are rendering a valid HTML page with at least <html> [...] </html> tags and a body. Do you?. Btw: I edited my answer. Still you need to render a proper html page. Commented Mar 19, 2018 at 14:17

2 Answers 2

1

How can I get the code to display both lines in a browser window?

Short answer: You have to render a minimal HTML page and format output according to HTML.


Let your script makevsce.php begin sending the client a minimal HTML page with the body "open":

<?php
header( "Content-Type: text/html; charset=utf-8" );

echo "<!DOCTYPE html>\n";
echo "<html>\n";
echo "<head>\n";
echo "<meta charset=\"utf-8\">\n";
echo "<title>Shell Output (or whatever...)</title>\n";
echo "</head>\n";
echo "<body>\n";

Let PHP properly escape the arguments injected in the string passed to the shell.

$mvdate  = escapeshellarg( $_POST['fdate'] );
$mvlevel = escapeshellarg( $_POST['flevel']);
$mvsite  = escapeshellarg( $_POST['fsite'] );

Use exec instead of shell_exec

exec( "/usr/local/bin/mvsce -d $mvdate -l $mvlevel -s $mvsite",
      $mvse,
      $return_value );

$mvse (passed by reference) will receive an array containing all the lines sent as output from the invoked command.

While the (optional) parameter $return_value holds the return value of the shell command.


To display the output:

foreach( $mvse as $line ) {
    echo htmlspecialchars( $line ) . "<br>\n";

Note that prior printing each output line text is properly HTML escaped since you're talking about displaying output in a browser.

At the end of each line a <br> will be interpreted by the browser as a line break.


Finally to close the HTML page:

echo "</body>\n";
echo "</html>";

Security notice:

Be aware you're executing via the shell a command created using input from the client.

A properly forged request may cause execution of arbitrary command with the same privileges as the PHP runtime.

Sign up to request clarification or add additional context in comments.

2 Comments

Still only reports the 2nd line, I have amended the question to truly reflect the bash output.
@JustinCooper-Marsh made several additions to my answer. Give it a look...
0

OK,

This is not an issue with PHP code.

If I write the output from the command to anything other than console it only writes the 2nd line.

Thanks for all your help.

2 Comments

This doesn't make much sense. Maybe the command is sending some output (the first line) to stderr. Try adding 2>&1 at the end of the command (the one issued from PHP)
/usr/local/bin/mvsce -d $mvdate -l $mvlevel -s $mvsite 2>&1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.