Header as redirector problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

@wyrmmage: you got some things mixed up.
session_start() by default generates a cookie to store the session id. Therefore
wyrmmage wrote:you must use the ob_start(); function (preferable after you use session_start().
can be confusing.
The "header() statement" in your example isn't one. It's within the string literal and will be printed as-is in the document, it has no special meaning and will not generate a http header. Both scripts will work without errors/warnings.
And even if it does generate a header - e.g. let's take the script

Code: Select all

<?php
session_start();
ob_start();
echo'
<HTML>
	<HEAD>
		<TITLE></TITLE>
	</HEAD>
	<BODY>';
header("Location: index.php");
echo '	</BODY>
</HTML>';
?>
What sense does it make to send a location head and document contents? The browser wouldn't display the contents anyway. It can only do one thing at a time: redirect, i.e. query another document, or display/process the current response document.
timvw wrote:The ob_* stuff will not buffer the output generated http headers
right, thats done even without the ob_* functions. http headers are stored and will be sent right before the first character f the response body is sent
wyrmmage
Forum Commoner
Posts: 56
Joined: Sat Oct 28, 2006 12:43 pm
Location: boise, ID

Post by wyrmmage »

hmmmm....looks like I got mixed up, sry for confusing anyone :oops:
However, ob_start() did solve the problems I was having a couple of months ago with redirection and I'm still using it, so let me get this right... Why would ob_* functions solve my redirection problems (I was using header(location))? Does the ob_start() function make it so that if a redirection in your code would occurr, no header information is sent, just the redirection?

-wyrmmage
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

a basic http request

Code: Select all

$s = fsockopen('www.php.net', 80) or die('socket error');

$req = "GET / HTTP/1.0\r\n" .
		"Host: http://www.php.net\r\n" .
		"User-Agent: php test script\r\n" . 
		"Accept: */*\r\n" . 
		"\r\n";

fputs($s, $req);

while(!feof($s)) {
	echo fread($s, 1024);
}
prints
HTTP/1.1 200 OK
Date: Sun, 29 Oct 2006 20:21:41 GMT
Server: Apache/1.3.37 (Unix) PHP/5.2.0-dev
X-Powered-By: PHP/5.2.0-dev
Last-Modified: Sun, 29 Oct 2006 20:26:40 GMT
Content-language: en
Set-Cookie: COUNTRY=DEU%2C88.72.31.65; expires=Sun, 05-Nov-2006 20:21:41 GMT; path=/; domain=.php.net
Connection: close
Content-Type: text/html;charset=utf-8

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
[...]
You can see the http headers before the actual document (starting with <!DOCTYPE html). The only delimiter between http headers and response body is the first empty line (after Content-Type:) That's why you cannot send headers after any response body contents. headers, empty line, contents. It's that simple.
ob_start() holds contents back in a buffer, it's not sent to the client. You can send headers as long as no contents is sent. Therefore you can use header()/session_start after an echo or print (...) if no contents has actually been sent because of ob_start().
Post Reply