[SOLVED] Cannot modify header information ?

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

Post Reply
FREESTYLER
Forum Newbie
Posts: 17
Joined: Tue Oct 18, 2005 10:33 am

[SOLVED] Cannot modify header information ?

Post 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?
Last edited by FREESTYLER on Thu Oct 27, 2005 7:24 pm, edited 1 time in total.
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

User avatar
harrisonad
Forum Contributor
Posts: 288
Joined: Fri Oct 15, 2004 4:58 am
Location: Philippines
Contact:

Post 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.
foobar
Forum Regular
Posts: 613
Joined: Wed Sep 28, 2005 10:08 am

Post by foobar »

<nitpick>
harrisonad wrote: or.. [...] just a whitespace
Newline is a whitespace.

</nitpick>
User avatar
harrisonad
Forum Contributor
Posts: 288
Joined: Fri Oct 15, 2004 4:58 am
Location: Philippines
Contact:

Post by harrisonad »

Yeah you're right. you are truly remarkable! :idea:
i'll give you an award for that.
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Post 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... :)
User avatar
harrisonad
Forum Contributor
Posts: 288
Joined: Fri Oct 15, 2004 4:58 am
Location: Philippines
Contact:

Post by harrisonad »

Hockey wrote:Arrogant...
Cool! 8)

any more description?
Post Reply