I am new at PHP, and I have a question about Output Buffering.I have this code I found on the net:
ob_start();
system('ipconfig /all');
$contents = ob_get_contents();
ob_end_clean();
$searchFor = "Physical";
$pmac = strpos($contents, $searchFor);
$mac = substr($contents, ($pmac + 36), 17);
return $mac;
It all works fine, but I don't understand the usage of the output buffer here.If I change it to:
$contents = system('ipconfig /all');
$searchFor = "Physical";
$pmac = strpos($contents, $searchFor);
$mac = substr($contents, ($pmac + 36), 17);
return $mac;
It can't seem to filter the contents of $contents to find the mac address.So what does output buffering do for this?
From what I understand about output buffering, it loads all of the page into a single variable, then returns it all at once so the page loads all at once and faster.I can't really see how this would change the output so drastically in this situation.
The system()
call will not just run a command, but also display the output. At the very least your sample code should have assigned the return value instead. Output buffering is furthermore needed, as the system
call will flush the command results directly to the webserver (unless there's an explicit output buffer).
What simply should have been used here instead is exec()
- with assigning the result to & $contents
right away.