Jump to content

[SOLVED] Cannot modify header information


Asday

Recommended Posts

I have an error, which I think is caused by include(), but I'm not too sure, being new.

 

Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\index.php:43) in C:\xampp\htdocs\counter.php on line 14

 

From what I can make out, the problem lies here:

 

//index.php
echo $txt1 . $txt2; // First thing that script echoes

 

and here:

 

//counter.php
if (isset($_COOKIE["user"]))
{
$fp=fopen("count.txt", "r+b");
$count194=fread($fp, filesize("count.txt"));
$count194 = "$count194";
for($x=0;$x<strlen($count194);$x++)
{
	echo '<img src="' . $count194[$x] . '.jpg">';
}
}
else
{
setcookie("user", time()+43200); // <---------------- Line 14
$fp=fopen("count.txt", "r+b");
$count194=fread($fp, filesize("count.txt"));
fclose($fp);
unlink("count.txt");
$count194++;
$count194 = "$count194";
for($x=0;$x<strlen($count194);$x++)
{
	echo '<img src="' . $count194[$x] . '.jpg">';
}

// echo $count194;

touch("count.txt");
$fpNew=fopen("count.txt", "r+b");
fwrite($fpNew, $count194);
}

 

EDIT:  Another thing, I don't get this error at home, only at work, and the cookie works at home, yet not at work.

Link to comment
Share on other sites

When you use echo or print, or something like that or when you get an error because a variable is not defined or something else, and then you use setcookie, there comes that error.

 

But all the variables I use are defined.

Link to comment
Share on other sites

You can find the solution here:

http://us2.php.net/manual/en/function.header.php

 

--

Tapos Pal

 

I've tried some of that, but it confuses me, and now I have 2 errors:

 

Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\funct.php:43) in C:\xampp\htdocs\counter.php on line 2

Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\funct.php:43) in C:\xampp\htdocs\counter.php on line 15

Link to comment
Share on other sites

2 Things here:

 

1. Your PHP configuration at home maybe suppressing warning messages or have a different output buffering setting.

2. You will always get that error whenever you echo or print something before trying to send a cookie to the browser.

 

;)

 

 

Link to comment
Share on other sites

2 Things here:

 

1. Your PHP configuration at home maybe suppressing warning messages or have a different output buffering setting.

2. You will always get that error whenever you echo or print something before trying to send a cookie to the browser.

 

or u can use

 

ob_start()

 

--

Tapos Pal

Link to comment
Share on other sites

 

 

or u can use

 

ob_start()

 

--

Tapos Pal

 

Where might I put that?

 

2 Things here:

 

...

2. You will always get that error whenever you echo or print something before trying to send a cookie to the browser.

 

So how to fix it?  How could I split the code up?

 

I have this:

 

<?php
include("funct.php"); // Where the echo lies
?>
// ...
<?php
include("counter.php");
?>

 

<?php
if (isset($_COOKIE["user"]))
{
$fp=fopen("count.txt", "r+b");
$count194=fread($fp, filesize("count.txt"));
$count194 = "$count194";
for($x=0;$x<strlen($count194);$x++)
{
	echo '<img src="' . $count194[$x] . '.jpg">';
}
}
else
{
setcookie("user", time()+43200);
$fp=fopen("count.txt", "r+b");
$count194=fread($fp, filesize("count.txt"));
fclose($fp);
unlink("count.txt");
$count194++;
$count194 = "$count194";
for($x=0;$x<strlen($count194);$x++)
{
	echo '<img src="' . $count194[$x] . '.jpg">';
}

// echo $count194;

touch("count.txt");
$fpNew=fopen("count.txt", "r+b");
fwrite($fpNew, $count194);
}
?>

Link to comment
Share on other sites

Change it like that:

<?php
include("counter.php");
include("funct.php"); // Where the echo lies
?>
// ...
<?php
echo $counter;
?>

And counter.php like that:

<?php
if (isset($_COOKIE["user"]))
{
$fp=fopen("count.txt", "r+b");
$count194=fread($fp, filesize("count.txt"));
$count194 = "$count194";
for($x=0;$x<strlen($count194);$x++)
{
	$counter = '<img src="' . $count194[$x] . '.jpg">';
}
}
else
{
setcookie("user", time()+43200);
$fp=fopen("count.txt", "r+b");
$count194=fread($fp, filesize("count.txt"));
fclose($fp);
unlink("count.txt");
$count194++;
$count194 = "$count194";
for($x=0;$x<strlen($count194);$x++)
{
	$counter '<img src="' . $count194[$x] . '.jpg">';
}

// echo $count194;

touch("count.txt");
$fpNew=fopen("count.txt", "r+b");
fwrite($fpNew, $count194);
}
?>

Link to comment
Share on other sites

Change it like that:

<?php
include("counter.php");
include("funct.php"); // Where the echo lies
?>
// ...
<?php
echo $counter;
?>

And counter.php like that:

<?php
if (isset($_COOKIE["user"]))
{
$fp=fopen("count.txt", "r+b");
$count194=fread($fp, filesize("count.txt"));
$count194 = "$count194";
for($x=0;$x<strlen($count194);$x++)
{
	$counter = '<img src="' . $count194[$x] . '.jpg">';
}
}
else
{
setcookie("user", time()+43200);
$fp=fopen("count.txt", "r+b");
$count194=fread($fp, filesize("count.txt"));
fclose($fp);
unlink("count.txt");
$count194++;
$count194 = "$count194";
for($x=0;$x<strlen($count194);$x++)
{
	$counter '<img src="' . $count194[$x] . '.jpg">';
}

// echo $count194;

touch("count.txt");
$fpNew=fopen("count.txt", "r+b");
fwrite($fpNew, $count194);
}
?>

 

But then the counter is at the top, whereas I want it at the bottom.

Link to comment
Share on other sites

If I'm not mistaken, you cannot send header data after you have echoed anything unless you clear the buffer.

 

ob_end_clean() should work if you just place it before the code that sends header information and after the code that would output data to the screen?

Link to comment
Share on other sites

If I'm not mistaken, you cannot send header data after you have echoed anything unless you clear the buffer.

 

ob_end_clean() should work if you just place it before the code that sends header information and after the code that would output data to the screen?

 

Tried that, and a challenger appears the same error persists.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.