![]() |
header VS include
I have been working with php for awhile. The problem i always come across is the argument between header and include commands. When i try to use the include above the header then a sentence of error takes place on the screen. Are there any ways to troubleshoot this problem ? as i have known header command doesn't get along with the include ;).
|
What error would that be?
|
The error is :
Warning: Cannot modify header information - headers already sent by (output started at C:\htdocs\checkinsValid\includes\function.php:2) in C:\htdocs\checkinsValid\checkin.php on line 70 |
Are you using sessions?
|
This error is caused because of the way the HTTP protocol works. Fundamentally, it works like this:
So you can see that if you try sending header information after the body has been processed, then this would naturally result in an error, as you're seeing there. To get around this you should really be aiming for an order of execution like so: Instead of how yours would currently be: However, if you don't want to follow that elementary order of execution then you may wish to take a look into the ob_* functions - with a nice start being the ob_start function. |
Quote:
Quote:
|
Eliminate any undesired white space prior or after <?php ?>.
Your error is saying: output started at c:\htdocs\checkinsValid\includes\function.php:2 So on line 2 your will find undesired white space or an echo that doesn't belong there. That will fix it. |
Quote:
|
Quote:
Ths for you suggestion..Devel :) |
Come on dude, my session_start() always stands on the peak ;)
|
The solution :
if this error happens when you use include then header Be sure you use : ob_start () at the most beginning of your code. ob_start () :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. and ob_flush() at the bottom of your codes ob_flush() : The buffer contents are discarded after this function is called. Hope this info can help others who meet such a problem :) |
There error isn't because you are using include(). It is because you are outputting data to the browser before PHP has finished processing all code that has to be handled before anything is sent to the browser.
Before anything gets sent to the browser you have to set the headers. You can't set them after you send data to the browser. That is why using header() does not work. |
Do not output any HTML after you use the header function, or you'll get the error. If your page has HTML tags in it, have the header function at the very top of the page, after the '<?php'
|
| All times are GMT. The time now is 12:25 AM. |
Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0