Honestly, If you had read post # 3 from jeniF, you would have known.

PHP - Warning: Cannot modify header information - headers already sent by
php header problem.
header('Location: http://www.php.net/');
cannot redirct webpage.
XAMPP PHP: 5.2.9
you must open php.ini (C:\xampp\php\php.ini)
find
output_buffering = off
change
output_buffering = 4096
restart xampp.
OK.
PHP de header komutu ile sayfa yönlendiremiyorum.
XAMPP PHP: 5.2.9 kullanıcısıyım.
C:\xampp\php\php.ini dosyasında
output_buffering = off
değerini
output_buffering = 4096
değiştirdikten sonra xampp yeniden başlattım.
Sorun çözüldü.

Thank you JaniF. I also joined DANIWEB because of you. You have helped me a lot with your expertise.

I Googled on this problem and found your simple solution, JeniF. Because of you, I joined DaniWeb. Thought you should know that even 2 years later your reply is still helping people!

Thanks, JeniF!! And thanks DaniWeb!

John

dont use ECHO function before using header. otherwise it will give error as you are saying.

I am using wordpress in localhost. i have deceloped a plugin, but it is not working

error msg is :
Warning: Cannot modify header information - headers already sent by (output started at /var/www/wordpress/wp-includes/functions.php:3641) in /var/www/wordpress/wp-includes/functions.php on line 791

Warning: Cannot modify header information - headers already sent by (output started at /var/www/wordpress/wp-includes/functions.php:3641) in /var/www/wordpress/wp-includes/functions.php on line 792


line 791: setcookie( 'wp-settings-' . $user->ID, $settings, time() + 31536000, SITECOOKIEPATH );
line 792: setcookie( 'wp-settings-time-' . $user->ID, time(), time() + 31536000, SITECOOKIEPATH );

Just wanted to do the same, thank jeniF for her simple yet grand solution. It's mid 2010, and it's still helping people-- THANK YOU!!!!

Paul

i have a problem just like you.

use this

if ($pass != $info['password']) 
{ ?>
<script language="javascript">location.replace("login.php"); </script>
<?
}

and use this too

else
//if the cookie does not exist, they are taken to the login screen
{ 
?>
    <script language="javascript">location.replace("login.php"); </script>
<?
}

The "headers already sent" error is usually caused by having white space before or after the opening and closing PHP tags (<?php . . . ?>).

Have you recently edited any files?

and alsu use top of page <?php ob_start(); ?> it remove all header error from form

thanks to jenif that worked perfectly. I just find this forum thread by googing.. It is very helpful. This solution is still work

Thanks a lot jenif...May Allah give your reward

I Googled on this problem and found your simple solution, JeniF. Because of you, I joined DaniWeb. Thought you should know that even 2 years later your reply is still helping people!

Thanks, JeniF!! And thanks DaniWeb!

John

Hi.
I am joining this community just to say thanks to JeniF, after 3 years, that answer is still helping people :)
Thanks all.

Just found your site and was having days of problems with a script I bought - a bit of bravery (I so am not technical) and I altered the script like you said and BINGO!!!! thank you so much. This will be my first port of call for any kinds of these problems.

thanks alot for JeniF, this problem has caused me alot of headache the whole day

If you are getting error like "Warning: Cannot modify header information - headers already sent by (output started at /path/file.php:1)" it means you've got some output before your header() call or starting a session. *No* output can be sent. that includes whitespace (outside of <?php and ?>) and any (X)HTML. You can also use output buffering

If you are getting error like "Warning: Cannot modify header information - headers already sent by (output started at /path/file.php:1)" it means you've got some output before your header() call or starting a session. *No* output can be sent. that includes whitespace (outside of <?php and ?>) and any (X)HTML. You can also use output buffering

i aggree, i had the same case. And i found a white space before <?php tags. And it have been succeded then my code is work :)

I was facing this problem for 2 moths.. Tried everything... nothing happens....

But finaly this blog helped me...... Thanx But can you please elaborate these two tags..

I mean what what is there working...

<? ob_start(); ?>


<? ob_flush(); ?>

Thanks JeniF!

I cant seem to find the error in my code and all I get is"Cannot modify header information - headers already sent by (output started at...". please help.

<?php

// Connects to your Database

mysql_connect("localhost", "root", "") or die(mysql_error());

mysql_select_db("employer") or die(mysql_error());


//checks cookies to make sure they are logged in
if(isset($_COOKIE))
{
$username = $_COOKIE;

$pass = $_COOKIE;

$check = mysql_query("SELECT * FROM user WHERE username = '$username'")or die(mysql_error());

while($info = mysql_fetch_array( $check ))

{

//if the cookie has the wrong password, they are taken to the login page

if ($pass != $info)

{ header("Location: login.php");

}

//otherwise they are shown the admin area

else

{

echo "Admin Area<p>";

echo "Your Content<p>";

echo "<a href=logout.php>Logout</a>";

}

}

}

else

//if the cookie does not exist, they are taken to the login screen

{

header("Location: login.php");

}

?>

I have tried all the suggested solutions but non of them is working. Could it be a problem of how I have saved my files?

Hi all. I tried many methods but it don't work.

I use denwer.
Look at my code and who can say please my error about (Warning: Cannot modify header information - headers already sent by (output started at Z:\home\localhost\www\Websiter\ChaT.php:1) in Z:\home\localhost\www\Websiter\ChaT.php on line 3)

This is my code

<?php
header("Location: google.ru");
?>

<html>
<head>
<title>Chat</title>

</head>

<body>

<?
if($_POST["chat_write"] != "") 
{
    $chattext = $_POST["chat_write"];
    $chatfile = fopen("chater.txt", "a+b") or die("Error");
    fwrite($chatfile, "$chattext\r\n");
    fflush($chatfile);
}
?>

<div style="font-weight:900"></div>

</div><div style="text-align:center; border:0.5em groove #666; background-color:#444; height:300px; margin-top:100px; width:500px; margin-left:auto; margin-right:auto;">
<?php
$handle = fopen("chater.txt", "a+b");

while(!feof($handle)) {
	$fileIn=fgets($handle);
    echo "<div style='color:orange; border:1px groove black; margin:1px; font-weight:500'>$fileIn</div>";
}
fclose($handle);
?>
</div>
<form name="chat_field" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">

<div align="center"><textarea name="chat_write" rows="3" cols="55"></textarea>
<input type="submit" name="sbmt" value="ChaT" /></div>

</form> 

</body>
</html>

Thanks Jenif The output buffer tags <? ob_start(); ?> before the php opening tag and <? ob_flush(); ?> after the php closing tag works perfectly. it eliminates the white spaces introduced by most editors. The white spaces before and after the php opening and closing tags are the culprits for this error.

for example:

<? ob_start(); ?>
            <?php
            $_SERVER[PHP_AUTH_USER] = htmlentities($_POST['uname']);
            $_SERVER[PHP_AUTH_PW] = htmlentities($_POST['upass']);
            // Connect to the MySQL database
                mysql_pconnect("localhost","root","")
                or die("Can't connect to database server!");
                mysql_select_db("airline_db")
                or die("Can't select database!");
                // Create and execute the selection query
                $query = "SELECT username, pswd FROM logins
                WHERE username='$_SERVER[PHP_AUTH_USER]' AND
                pswd='$_SERVER[PHP_AUTH_PW]' ";
                $result = mysql_query($query);
                // If nothing was found, reprompt the user for the login information
                if (mysql_num_rows($result) == 0) {
                echo "Invalid User";
                } else {
                    header("location: bookflight.html");
                    exit();
                  }
                 ?>
<? ob_flush(); ?>

Hi HM0729, enclose your php code blocks inbetween <? ob_start(); ?>
and <? ob_flush(); ?> Should work fine. Cheers

Hi all...
I'm reading this solution in 2011...and it's still usefull...
thanks JeniF....it's the solution that I looking for... :)

Thanks JeniF for this solutions

Regards,
Santhosh

This solution is so helpful for me, i have faced this problem since long time, and now i resolved this issue, thank u very much.

I am so much happy to signin in this site. Many technical problem Now I can solve .
Thanks again.............

Thanks, JeniF!! helped a lot

<?php ob_start("ob_gzhandler"); ?> Instant compressed code, as well as space correction
perhaps a better solution

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.