Problem using php header() function

fguy64

New Member
Messages
218
Reaction score
0
Points
0
Hi this is day1 of my php education, and I'm running into problems early. I'm trying a simple redirect based on correct entry of a password. I checked a few sources and my code seems ok, but...

first my html form...

Code:
<form action="success.php" method="post">
Password: <input type="password" name="password"> <input type="submit" />
</form>

and here is my success.php

Code:
<?php

$pwd = $_POST["password"];

if( $pwd == "123" ) {
	header( "Location: success.htm" );
} else {
	echo "nyeh";
}

?>

when I enter the correct password, I get...

Warning: Cannot modify header information - headers already sent by (output started at /.../.../success.php:8) in /.../.../.../success.php on line 13
 
Last edited:

gomarc

Member
Messages
516
Reaction score
18
Points
18
The problem may be with success.htm. Your code as posted works fine.

If success.htm =

HTML:
<html>
  <body>
  SUCCESS!  
  </body>
</html>
... it will work.
 

fguy64

New Member
Messages
218
Reaction score
0
Points
0
nah, I double checked and tried again, same problem. The only thing I can think of is the first time I executed the script, I had forgotton to upload my success.htm, but it's there now.

When I checked the php reference for this error, it made reference to some kind of flush() command, but it didn't look like something a newbie had access to, and I didn't try it.

you can find the error here

http://ca.php.net/manual/en/function.flush.php

I wasn't sure if I should try it without a second opinion, maybe it affects the servers?

edit: I should mention that my php file starts an ends with the usual html tags, but I don't think that should be a problem, here is teh entire file

Code:
<html>
<head>
<title></title>
</head>

<body>

<?php

$pwd = $_POST["password"];

if( $pwd == "123" ) {
	header( "Location: success.htm" );
} else {
	echo "nyeh";
}

?>

</body>
</html>
Edit:
ok, I took all the html tags out of my php file and it worked. hmm I thought you could embed php within html tags, but maybe I misread. It's getting late...
 
Last edited:

gomarc

Member
Messages
516
Reaction score
18
Points
18
Yes, it does make a difference! You can embed php within html tags, but the header() statement must go before any output, in this case <head><title></title></head>.
 

fguy64

New Member
Messages
218
Reaction score
0
Points
0
Yes, it does make a difference! You can embed php within html tags, but the header() statement must go before any output, in this case <head><title></title></head>.

I'm not sure I get what you are saying. the tags you mention constitute output? So I tried as below and the problem returned. I made sure there was no white space before or after my php tags.


<html>
<body>
<?php

$pwd = $_POST["password"];

if( $pwd == "123" ) {
header( "Location: success.htm" );
} else {
echo "nyeh";
}

?>
</body>
</html>
 

Livewire

Abuse Compliance Officer
Staff member
Messages
18,169
Reaction score
216
Points
63
I'm not sure I get what you are saying. the tags you mention constitute output? So I tried as below and the problem returned. I made sure there was no white space before or after my php tags.


<html>
<body>
<?php

$pwd = $_POST["password"];

if( $pwd == "123" ) {
header( "Location: success.htm" );
} else {
echo "nyeh";
}

?>
</body>
</html>

As soon as any text (even a blank space) is returned, PHP must send headers that indicate a standard HTML webpage.

So, in short, to use header, the very first characters in your document MUST be <?php - this particular string isn't sent to the user, and it just sets it up to process data; unless you echo/print anything to the screen (or a function throws an error and echo/prints it), you can use header to send different headers other than a standard webpage :)
 

fguy64

New Member
Messages
218
Reaction score
0
Points
0
ok, so php with header() function is like an exception to the rule.

thanks to both for the help.
 

xav0989

Community Public Relation
Community Support
Messages
4,467
Reaction score
95
Points
0
Since you are just starting, this can help you point out that the best practice in coding is to separate code from layout/content. Have a code file that sends headers, then load content, then display it.
 

fguy64

New Member
Messages
218
Reaction score
0
Points
0
Since you are just starting, this can help you point out that the best practice in coding is to separate code from layout/content. Have a code file that sends headers, then load content, then display it.

yeah, now that you mention it, I do recall reading something like that somewhere. Makes sense and duly noted, thanks.
 

xav0989

Community Public Relation
Community Support
Messages
4,467
Reaction score
95
Points
0
that's all the MVC (Model->content, View->layout, Controller->code) thing that developers are praising
 

misson

Community Paragon
Community Support
Messages
2,572
Reaction score
72
Points
48
ok, so php with header() function is like an exception to the rule.

header() isn't an exception to anything. Headers set by header() are HTTP headers and need to be sent before content. Once you output the response body, there's no way to print any more headers, which is why headers() will complain. Output buffering will let you store output without sending it to the browser, meaning you can still add headers, but buffering isn't always appropriate. For instance, when you're using the "Location" header, the response body will be ignored so buffering will merely result in wasted processing.
 

fguy64

New Member
Messages
218
Reaction score
0
Points
0
header() isn't an exception to anything. Headers set by header() are HTTP headers and need to be sent before content. Once you output the response body, there's no way to print any more headers, which is why headers() will complain. Output buffering will let you store output without sending it to the browser, meaning you can still add headers, but buffering isn't always appropriate. For instance, when you're using the "Location" header, the response body will be ignored so buffering will merely result in wasted processing.

It's an exception to the general rule that says php code can be embedded within html, no? That's what I meant, anyways.

Thanks for the info though, live and learn :)
 
Last edited:

misson

Community Paragon
Community Support
Messages
2,572
Reaction score
72
Points
48
It's an exception to the general rule that says php code can be embedded within html, no?
No. Having content output before trying to call header() doesn't affect embedding (the embedded PHP code is still interpreted by PHP), it just causes the function call to fail.
 

fguy64

New Member
Messages
218
Reaction score
0
Points
0
No. Having content output before trying to call header() doesn't affect embedding (the embedded PHP code is still interpreted by PHP), it just causes the function call to fail.

hmmm one of us is missing the point, or I am just expressing myself poorly. I don't mean to belabor the point but let me try to express this differently.

there are many examples of php functions that can be called from within an html document, right?

for example the following will work, right?...

<html>
<body>
<?php echo "hello" ; ?>
</body>
</html>

but if I try to put a header() call within those html tags, that function call will fail, right?

if I am right so far, then that makes header() an exception according to my definition of the word exception.
 
Last edited:

misson

Community Paragon
Community Support
Messages
2,572
Reaction score
72
Points
48
hmmm one of us is missing the point, or I am just expressing myself poorly. I don't mean to belabor the point but let me try to express this differently.
It's partially a matter of definitions. "Embedding PHP" means a PHP can be placed in a document of another (formal) language (the "host" language).

Also, you've never explicitly stated the rule. If your rule is (A) "PHP can be embedded in HTML," then header() is not an exception (according to the above definition of embedding PHP). If your rule is (B) "Any function call in PHP can succeed at any point in every PHP script", then header() is an exception, as are setcookie() and many DB access functions. For example, if you don't call mysql_connect(), then mysql_query() won't work. Similarly, if you use the output buffering control functions, you can use header() and setcookie() at any point before you flush the output buffer. Rule (B) is not one I've heard expounded before and doesn't seem useful.

If you're thinking of a different rule, note the similar relationships between ob_start() & header() and mysql_connect() & mysql_query(). Keep these relationships in mind and check whether mysql_query() is also an exception.
 
Last edited:

ichwar

Community Advocate
Community Support
Messages
1,454
Reaction score
7
Points
0
hmmm one of us is missing the point, or I am just expressing myself poorly.
no, I get what you're asking. yes the php header() is an exception to the general rule that php can be put inside of html tags. For the header to work, the <?php that starts the code must be the first 5 characters on the page. So yes, header() is an exception.
 

misson

Community Paragon
Community Support
Messages
2,572
Reaction score
72
Points
48
yes the php header() is an exception to the general rule that php can be put inside of html tags.
It can, if you use output buffering. The more accurate (and helpful) rule is that header() will fail if any part of the response body (as opposed to headers) has been printed.
 
Top