This tutorial will show you how to make a php script to run server commands ( e.g. "ls" ). If you want to learn linux commands check out our YouTube Channel.
We are going to need two files for this, let’s call them cli.html and cli.php.
All we need to have in our html file is a textbox, a div where our command results will appear and of course JQuery for AJAX.
These are the contents of cli.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<form>
<input type="text" id="command" />
</form>
<div id="cli">
</div>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js" type="text/javascript">
</script>
<script type="text/javascript">
$(document).ready(function(){
$("form").submit(function(){
$.post('cli.php',{command:$("#command").val()},function(e){
$("#cli").html(e);
$("#command").val('');
});
return false;
});
});
</script>
</body>
</html>
We don’t need a button in the form because, like in any command prompt, the command is executed when you hit enter and the text is cleared. ( Note: JQuery’s submit function might not work in Internet Explorer )
Once we have sent the command to the php file we will execute the command by echoing it, not inside quotes, but with backticks ” ` ` ” (these are not single quotes).
So this is the content of our php file.
$command=$_POST[command]; echo '<pre>'; echo`$command`; echo '</pre>';
You are ready to go! Now you just need to make the background black and the font green to feel like you really are using a terminal. Oh and one more thing, do not let users access your command prompt.