I am wondering is this safe way to put ps aux into array and then display on the web? Or what could be done to improve it?
Example:
<table width="900px" border="1">
<tr>
<td> PID </td>
<td> CPU </td>
<td> Mem </td>
<td> Start </td>
<td> Command</td>
</tr>
<?php
exec("ps aux | grep -v grep | grep process.php", $psOutput);
if (count($psOutput) > 0) {
foreach ($psOutput as $ps) {
$ps = preg_split('/ +/', $ps);
$pid = $ps[1];
$cpu = $ps[2];
$mem = $ps[3];
$time = $ps[8];
$command = $ps[10] . " " . $ps[11];
echo "<tr>";
echo "<td>" . $pid . "</td>";
echo "<td>" . $cpu . "</td>";
echo "<td>" . $mem . "</td>";
echo "<td>" . $time . "</td>";
echo "<td>" . $command . "</td>";
echo "</tr>";
}
}
?>
</table>
Nothing as far as I can tell. If this is the actual code and the command isn’t created from user input, there is absolutely nothing wrong with this code, apart from the fact that
<table width="900px">is generally controlled by CSS, not HTML. But that’s all the critique I can think of.EDIT: Quentin makes a very valid point in that you should use htmlspecialchars before displaying in HTML.