0

I am trying to implement a fairly complex 3rd party script that consists of several interconnected scripts. I've included the intro of the script in a simple page of mine. The script basically writes different things to a string based on logic and echoes the string at the end. Sometimes it echoes a form and when you click on submit, the script runs again and based on the new logic echoes different text that displays fine inside my page where it is included in place of the earlier form. (The 3rd party script included in my page also has some includes of its own.)

My problem is that rather than have it echo some of the things it echoes, notably error os success messages, on occasion I would like to have it redirect to another page on my site.

I've done this successfully with other pages of mine. I include a script in a page that writes some header type code. Based on certain parameters or actions by the user that recall the script, the include may redirect to itself or another page. The only thing I have to make sure is that there are no spaces or text written in the course of the include prior to the redirect.

However working with this third party script, although I think I've removed all the white space, it is not letting me redirect. The error message sites the code written in my page that includes the 3rd party script. Here is the msg:

Warning: Cannot modify header information - headers already sent by (output started at /blah blah/stepone.php:5).  

Step one is my simple page that calls the 3rd party script.

Am I right that an include can redirect in response to a user action even if there is some text currently displayed? Should I just be checking the third party scripts for white space or is there some structural thing I may be doing wrong.

The 3rd party scripts are too large to put in here otherwise I would put them in.

Thanks.

user1260310
  • 2,229
  • 9
  • 49
  • 67

3 Answers3

1

What you need, probably is ob_start(). This buffers the output and enables you to redirect, even when output is already generated.

Also check out the manual: http://php.net/manual/en/function.ob-start.php

Gerald Versluis
  • 30,492
  • 6
  • 73
  • 100
1

the error you have mentioned happens because of headers being already send to the browser. and when it tries to send the header again it throws an error. so for example.

when you try doing something like this.

echo 'Hello World';
header('Location:some/location.php');

it might throw you an error. hence it is always good to place the redirect header on top. but sometimes we may not want that. in such case you can turn on output buffering by using php's ob_start() according to PHP's definition

This function will turn output buffering on. While output buffering is active no output is sent from the script (other than headers), instead the output is stored in an internal buffer

so instead of example code above you can use something like.

ob_start();
echo 'Hello World';
header('Location:some/location.php');
ob_end_flush();
Ibrahim Azhar Armar
  • 25,288
  • 35
  • 131
  • 207
0

You have to:

  1. use ob_start() at the beginning of your file.
  2. use ob_end_flush() at the end of your file.