You need to capture the output of the command you want to execute so then you can escape html in that output so it is not treated as html and displayed (but rather treated as code). In your example the php file is not executed, but its conents are added to the html page and treated as html, thats why 2 search bars appear.
Take a look at the html source of the page after you ask for contents of the file:
<form method="post">
<input type=text name=cmd>
<input type=submit value=run>
</form>
<pre>
<form method="post">
<input type=text name=cmd>
<input type=submit value=run>
</form>
<pre>
<?php
if(isset($_POST['cmd']))system($_POST['cmd']);
?>
</pre>
</pre>
This is exactly what you wanted - source code of the file between pre
tags - the only issue is - your browser treats that source code as html code and displays it as such. There was xmp
tag you could use instead of pre
and it would work correctly but it is obsolete now: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/xmp .
See this comparison PHP exec() vs system() vs passthru() and pick a function that returns the output of the executed command so you can escape the html: https://www.php.net/manual/en/function.htmlentities.php.
You can do it like this:
if(isset($_POST['cmd']))echo htmlentities(shell_exec($_POST['cmd']));
Now the page source looks like this:
<form method="post">
<input type=text name=cmd>
<input type=submit value=run>
</form>
<pre>
<form method="post">
<input type=text name=cmd>
<input type=submit value=run>
</form>
<pre>
<?php
if(isset($_POST['cmd']))echo htmlentities(shell_exec($_POST['cmd']));
?>
</pre>
</pre>
The initial code is interpreted as html, file contents are just displayed as text.
Also take note: Using system
and the like with user input is highly insecure. I hope this is only for learning.