Asday Posted July 9, 2007 Share Posted July 9, 2007 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 https://forums.phpfreaks.com/topic/59057-solved-cannot-modify-header-information/ Share on other sites More sharing options...
Lumio Posted July 9, 2007 Share Posted July 9, 2007 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. Link to comment https://forums.phpfreaks.com/topic/59057-solved-cannot-modify-header-information/#findComment-293162 Share on other sites More sharing options...
Asday Posted July 9, 2007 Author Share Posted July 9, 2007 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 https://forums.phpfreaks.com/topic/59057-solved-cannot-modify-header-information/#findComment-293165 Share on other sites More sharing options...
tapos Posted July 9, 2007 Share Posted July 9, 2007 You can find the solution here: http://us2.php.net/manual/en/function.header.php -- Tapos Pal Link to comment https://forums.phpfreaks.com/topic/59057-solved-cannot-modify-header-information/#findComment-293180 Share on other sites More sharing options...
Asday Posted July 9, 2007 Author Share Posted July 9, 2007 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 https://forums.phpfreaks.com/topic/59057-solved-cannot-modify-header-information/#findComment-293182 Share on other sites More sharing options...
matto Posted July 9, 2007 Share Posted July 9, 2007 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 https://forums.phpfreaks.com/topic/59057-solved-cannot-modify-header-information/#findComment-293183 Share on other sites More sharing options...
tapos Posted July 9, 2007 Share Posted July 9, 2007 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 https://forums.phpfreaks.com/topic/59057-solved-cannot-modify-header-information/#findComment-293186 Share on other sites More sharing options...
Asday Posted July 9, 2007 Author Share Posted July 9, 2007 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 https://forums.phpfreaks.com/topic/59057-solved-cannot-modify-header-information/#findComment-293219 Share on other sites More sharing options...
Lumio Posted July 9, 2007 Share Posted July 9, 2007 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 https://forums.phpfreaks.com/topic/59057-solved-cannot-modify-header-information/#findComment-293228 Share on other sites More sharing options...
Asday Posted July 9, 2007 Author Share Posted July 9, 2007 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 https://forums.phpfreaks.com/topic/59057-solved-cannot-modify-header-information/#findComment-293230 Share on other sites More sharing options...
soycharliente Posted July 9, 2007 Share Posted July 9, 2007 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 https://forums.phpfreaks.com/topic/59057-solved-cannot-modify-header-information/#findComment-293234 Share on other sites More sharing options...
Asday Posted July 9, 2007 Author Share Posted July 9, 2007 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 https://forums.phpfreaks.com/topic/59057-solved-cannot-modify-header-information/#findComment-293237 Share on other sites More sharing options...
Asday Posted July 9, 2007 Author Share Posted July 9, 2007 Bump. Link to comment https://forums.phpfreaks.com/topic/59057-solved-cannot-modify-header-information/#findComment-293346 Share on other sites More sharing options...
Yesideez Posted July 9, 2007 Share Posted July 9, 2007 http://www.phpfreaks.com/forums/index.php/topic,37442.0.html Have you read that? Link to comment https://forums.phpfreaks.com/topic/59057-solved-cannot-modify-header-information/#findComment-293347 Share on other sites More sharing options...
Asday Posted July 9, 2007 Author Share Posted July 9, 2007 Oh dear. Now I seem like a forum newbie. :'( sorry everyone, read that a few lines down and solved the problem. I shall now go find a good smacking to teach me not to do that again. Link to comment https://forums.phpfreaks.com/topic/59057-solved-cannot-modify-header-information/#findComment-293360 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.