Manngo Manngo - 1 year ago 81
PHP Question

PHP: Demonstrate Headers Already Sent

Note: I am not trying to solve the

headers already sent
problem. I am trying to create one!

I thought I understood what causes this error, and I am trying to demonstrate it to some students. Here is a sample script:



print 'This space for rent …';


As you see, I am trying my best to send output before setting a header, either indirectly by setting a cookie, or directly using the

As weird as the question sounds, how can I get this error to appear?

Answer Source

If output buffering is on in the php.ini it won't throw the error as output is captured and sent at the end. This is sort of like the "lazy man's" way to prevent these errors instead of actually fixing the scripts causing them. You can create the error by turning this off or ending output buffering before the output with something like ob_end_clean() (then output, then header)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download