nirVaan Posted August 10, 2009 Share Posted August 10, 2009 hi i have heard and seeing every one telling that the header function must be called before and out put to the browser. but the CAPTCHA code tutorials that i see calls the headrer function in its scrip like header("Content-Type: image/jpeg"); but this script is called by the main script in a code line such as < td > Enter text seen in this image < br/ > < img src=”img/captcha.php?nocache= < ?php echo time(); ? > ” alt=””/ > < br / > < input type=”text” name=”captcha” id=”captcha”/ > < /td > so my problem is isnt before the header("Content-Type: image/jpeg") is called other html tags have been outputed to the browser like < td > Enter text seen in this image < br/ > so how does this work and another question is how exactly does a white space or empty lines out putted to the browser im new to php so this is bit confusing to me so please explain thanks Link to comment https://forums.phpfreaks.com/topic/169565-need-some-explanation-on-the-header-function-i-did-read-the-header-error-post/ Share on other sites More sharing options...
GingerRobot Posted August 10, 2009 Share Posted August 10, 2009 so my problem is isnt before the header("Content-Type: image/jpeg") is called other html tags have been outputed to the browser like < td > Enter text seen in this image < br/ > No, that header is sent on a separate request to the webserver. Headers (amongst other things) tell the browser what kind of data is being sent. When you load the page with the form on, your browser makes a request to the webserver, which serves the webpage. You may or may not have sent a header with this response and, if you haven't, the browser will guess that it's supposed to be HTML. One of the things the page contains is an image. The browser must then fetch that image also, which is a separate request. A different request may, of course, be for different content. and another question is how exactly does a white space or empty lines out putted to the browser Whitespace in your PHP/HTML code are output to the browser, although they don't "do" anything. That is why you'll not see them unless you view the source of the webpage in your browser or output the php code to a file/stream. Link to comment https://forums.phpfreaks.com/topic/169565-need-some-explanation-on-the-header-function-i-did-read-the-header-error-post/#findComment-894642 Share on other sites More sharing options...
nirVaan Posted August 10, 2009 Author Share Posted August 10, 2009 let me see whether i undestand what you are saying.let say i have a webpage and in that in the middle of some tags i calls the header function.since a header is already sent. but instead i have link to a different php file even as the img src since the web browser have to make a seperate request it will be ok is it. but i still dont quite undestand how white spaces are sent i mean we indent code writing any programming language so does that white spaces sent to the browser ???? Link to comment https://forums.phpfreaks.com/topic/169565-need-some-explanation-on-the-header-function-i-did-read-the-header-error-post/#findComment-894809 Share on other sites More sharing options...
GingerRobot Posted August 10, 2009 Share Posted August 10, 2009 but i still dont quite undestand how white spaces are sent i mean we indent code writing any programming language so does that white spaces sent to the browser ???? No, that wouldn't send whitespace to the browser because it would be interpreted(or, more accurately, ignored) by the PHP engine. You get whitespace in any HTML that you might send to the browser. One of the important things to recognise is that anything not contained within PHP tags is considered HTML. So the following code fragment would send a line of whitespace: <?php //code ?> <?php //more code ?> As would the following code (which is often the cause of the header error): <?php //code ?> It's more difficult to spot, since it's at the top of the code. Further, in included files, you shouldn't have any whitespace after the closing PHP tag, as this could cause you troubles.. Indeed, it's valid (and recommended by some coding standards) to leave off the last closing PHP tag of a file if there's no HTML to follow to avoid this problem. Link to comment https://forums.phpfreaks.com/topic/169565-need-some-explanation-on-the-header-function-i-did-read-the-header-error-post/#findComment-894854 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.