TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   General (http://www.talkphp.com/general/)
-   -   header VS include (http://www.talkphp.com/general/1456-header-vs-include.html)

vuthcam 11-14-2007 03:02 PM

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 ;).

Wildhoney 11-14-2007 03:10 PM

What error would that be?

vuthcam 11-14-2007 03:20 PM

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

Haris 11-14-2007 03:22 PM

Are you using sessions?

Wildhoney 11-14-2007 03:42 PM

This error is caused because of the way the HTTP protocol works. Fundamentally, it works like this:

User (Request Page) -> Server(Processes Request)
Server (Send Header Data) -> User (Interprets Header)
Server (Send Body Data) -> User (Parses Body)
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:

Process PHP -> Process JS -> Process HTML -> Process CSS
Instead of how yours would currently be:

Process PHP -> Process JS -> Process HTML -> Process CSS -> Process PHP -> Process HTML
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.

vuthcam 11-14-2007 03:52 PM

Quote:

Originally Posted by Haris (Post 4094)
Are you using sessions?

Yeah Haris!! i am using sessions.

Quote:

Process PHP -> Process JS -> Process HTML -> Process CSS
i dont really catch up this sentence. Please elaborate me more..

Devels 11-14-2007 04:08 PM

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.

Haris 11-14-2007 04:12 PM

Quote:

Originally Posted by vuthcam (Post 4097)
Yeah Haris!! i am using sessions.



i dont really catch up this sentence. Please elaborate me more..

Make sure session_start(); is at the top of the file.

vuthcam 11-14-2007 04:20 PM

Quote:

Originally Posted by Devels (Post 4100)
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.

I think this is not caused by the white space in this situation meanwhile no white space prior of after <?php ?>. The problem is the include and header. I am sure about the error but i cannot fix that problem hehe.. as i have read somewhere before, they said that we cannot this problem usually caused by using the include before the header command..

Ths for you suggestion..Devel :)

vuthcam 11-14-2007 04:22 PM

Come on dude, my session_start() always stands on the peak ;)

vuthcam 11-14-2007 05:30 PM

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 :)

wGEric 11-14-2007 05:52 PM

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.

CMellor 11-14-2007 08:51 PM

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