0

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.

shankar.parshimoni
  • 1,289
  • 5
  • 22
  • 42
Daniel
  • 13
  • 4

1 Answers1

0

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.

mario
  • 144,265
  • 20
  • 237
  • 291
  • Thanks! So at the very basic level, the output buffer catches the output of system() before it is outputted (is that even a word? :P), and then writes the value to the string, and then filters it? – Daniel Dec 10 '14 at 04:21