Link to home
Start Free TrialLog in
Avatar of lawrence_dev
lawrence_dev

asked on

How do I execute a file from within a PHP script with headers already sent?

How do I execute a file from within a PHP script with headers already sent?    

I have this code at the bottom of a php script.  However, I am getting a "Headers Already Sent" error.  Is there a way to get around that?

$path = '/home/*********/public_html/*********/Feed-Master.php';
exec($path, $output,$return);
var_dump($return);

Open in new window

Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Yes.  You can use ob_start() at the top of your script to capture the output and buffer it until the end of the script.  Either that, or reorganize the code to produce the output in the correct order.  Usually, ob_start() is easier.
https://www.experts-exchange.com/articles/4423/Warning-Cannot-modify-header-information-headers-already-sent.html
Avatar of lawrence_dev
lawrence_dev

ASKER

Ray,
I placed ob start at the top of the page and that returned the headers already sent error.  So I moved it to the bottom and received the same error.

Warning: Cannot modify header information - headers already sent by (output started at /home/*******/public_html/*******/ProductFeed-Master.php:2) in /home/*********/public_html/*******/ProductFeed--Master.php on line 308  

ob_start();
header('/home/******/public_html/********/BingFeed-Master.php');
exit();

Open in new window

SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I got it to work.     Thanks for your help!


ob_start();
$return = include '/home/*******/public_html/*******/BingFeed-Master.php'; 
$return = ob_get_clean();

Open in new window