Jump to content
xisto Community
MacFly

Question On Header() Function

Recommended Posts

Hello I have a question regarding the header(location:http:// etc) function. I have a php document that looks similar to the followiing <? include("./top.php"); //this file contains loads of html and is resident on all pages in the site IT HAS TO BE HERE //now some code specific to this page only if blah blah { do some stuff } else { header("location : URL"; } include ("./bottom.php"); //this file contains loads of html and is resident on all pages in the site IT HAS TO BE HERE ----- my question is this, how can i redirect to a new page using the header() function in the middle of a file like this ie when i have already passed a load of html and other stuff to the browser. When I run the above i get the following error Warning: Cannot modify header information - headers already sent by (output started at /var/www/html/include/top-left.inc.php:129) in /var/www/html/comments.php on line 19 I am reasonably new to PHP and after investigation found out that to use the header() redirect there can not be any html or white space sent to the browser before the actual header() command Im sure there is a really simple solution to this but im a bit stumped and would appreciate some help.

Share this post


Link to post
Share on other sites

Because the header() function sends raw headers, it has to be called before any other data is sent to the client - eg. via the echo() or printf() functions.

 

My guess is that the script top.php (which is being called first) is sending some kind of output - you will have to cancel this if you want to send headers.

 

greenmask's example should work fine.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.