Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

PHP

Milos Stankovic
Milos Stankovic
15,691 Points

Redirection to the thanks.php page is not working

On my localhost (php 5.6), the redirection to the thanks.php page is not working. I got error:

"headers already sent..."

This is fixed when I delete all echo lines in process.php file.

Check this...

2 Answers

Hi Milos,

Which course is this from and can you link to the video you're currently on?

This is the expected behavior if output buffering is turned off. Header redirects will not work if you have any kind of output to the page before the header redirect. The echo statements you have as well as a blank line at the top of the file before the opening php tag would all count as output to the page and stop a redirect from happening.

If output buffering is turned on then the header redirect would still work because the output is buffered instead of being sent.

You likely have output buffering turned off in your local installation. The php environment within workspaces has output buffering turned on. So it should work in workspaces even with the echo statements in place.

If you'd like to see what your output_buffering directive is set to you can add phpinfo(INFO_CONFIGURATION); to the top of your page and it should give you a table of directives and their values.

Look for output_buffering and see what the value is. If it's 'no value' or 'off' or maybe even 0 then it means output buffering is turned off.

Milos Stankovic
Milos Stankovic
15,691 Points

Hi Jason,

Thank you for the suggestion. When I enabled output_buffering in php.ini, now page redirects even with echo commands uncommented.

https://teamtreehouse.com/library/build-a-basic-php-website/adding-a-basic-form/redirecting-after-a-form-submission

Cheers, Milos