Page 1 of 1

[SOLVED] Cannot modify header information ?

Posted: Sun Oct 23, 2005 10:40 pm
by FREESTYLER
hey gusy, i made this login and auth part for my website. learnt how to do it from my php book i have ("Wrox - beginning php5, apache, mysql web development")
everything worked fine when i had only the auth on my mail index.php page, butn nbow i get this error on the login page

ERROR:
Warning: Cannot modify header information - headers already sent by (output started at /home2/datainfi/public_html/geovis/channel7/login.php:6) in /home2/datainfi/public_html/geovis/channel7/login.php on line 16

its on this line of code

Code: Select all

header ("Refresh: 3; URL=" . $_POST['redirect'] . "");
any ideas?

Posted: Sun Oct 23, 2005 10:42 pm
by Jenk
You're receiving that error because there has already been some output sent to the client, you need to ensure headers are sent before ANY output, that includes whitespace, is sent.

Posted: Sun Oct 23, 2005 10:47 pm
by feyd

Posted: Sun Oct 23, 2005 11:39 pm
by harrisonad
you may have start your PHP file this way:

Code: Select all

<?php
new line before php opening tag
or..

Code: Select all

<?php
just a whitespace

try checking that one.

now if you have no problem with that, check the way you use echo() function.
I completely recommend to put your HTML strings in a variable first then echo it when you have nothing more to do such as conditions and loops. This way you can freely use the header() function without any error.

for example:

Code: Select all

$willredirect = true;

$str = "
my TODO list:
<ul>";
for ($i=0;$i<=100;$i++) {
    $str .= "
    <li>I will read before posting...</li>";
}
$str .= "
</ul>";

if ($willredirect) {
   header('location: finalpage.php');
} else {
    echo $str;
}
Hope that helps.

Posted: Mon Oct 24, 2005 10:26 am
by foobar
<nitpick>
harrisonad wrote: or.. [...] just a whitespace
Newline is a whitespace.

</nitpick>

Posted: Thu Oct 27, 2005 7:07 pm
by harrisonad
Yeah you're right. you are truly remarkable! :idea:
i'll give you an award for that.

Posted: Thu Oct 27, 2005 8:10 pm
by alex.barylski
harrisonad wrote:Yeah you're right. you are truly remarkable! :idea:
i'll give you an award for that.
:lol:

I'm only laughing at the harsh reality that developers...are so damn...whats the word...

Arrogant...?? :)

I'm guilty of making silly corrections, when I know it's not really necessary...but I do anyways... :)

Posted: Thu Oct 27, 2005 9:15 pm
by harrisonad
Hockey wrote:Arrogant...
Cool! 8)

any more description?