1

Newbie here with weird error. Before I start - I've read and understood (at least I think so) this topic: How to fix "Headers already sent" error in PHP which has great overview of this error, but I did not find solution there. Now the problem: I have 2 simple .php files (05.php and 10.php)... both of them contain 5 table rows. First 3 columns are not relevant (simple text). Fourth column reads content of .txt file and displays it. Fifth column has 2 submit buttons("+" and "-") which basicly open .txt file, read number in it, changes it's value, replace value in file with changed one and refreshes site. I need to include both of those files in my "main.php" file.

When I open them separetly, they work fine, when I include any of them in "main.php", everything works fine, but when I include both of them, input buttons on firt 7 rows work fine, but buttons on rows 8-10 give me this error:

Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\test\10.php:94) in C:\xampp\htdocs\test\10.php on line 120

Second line number changes when I press different input buttons (which is obvious). First line number is always the same, and points to line with this code :

</form>

I cannot figure out what's wrong... I know that this code is not "nice" and best way of doing this kind of stuff, but like I said... newbie on deck :) Here are full codes of "05.php", "10.php" and "main.php"...

"05.php"

    <tr>
      <td align="right" valign="middle">Papier półpołysk klej standard</td>
      <td align="right" valign="middle">DIG UV INKJET MC80 FSC S2000N-BG40BR</td>
      <td align="right" valign="middle">0,166</td>
<td align="right" valign="middle">
<?php $fp01 = fopen("licznik01.txt", "r+");
$count01 = fgets($fp01);
fclose($fp01);
echo $count01;
?>
</td>
<td align="center" valign="middle">
<form method="post">
<input class="czerwony" name="minus01" id="minus01" value="-" type="submit">
<?php function odejmij01()
{
$fp01 = fopen("licznik01.txt", "r+");
$count01 = fgets($fp01);
$count01 = $count01 - 1;
ftruncate($fp01, 0);
fseek($fp01, 0);
fputs($fp01, $count01);
fclose($fp01);
header("Refresh:0");
}
if(array_key_exists('minus01',$_POST)){
odejmij01();
}
?>
<input class="zielony" name="plus01" id="plus01" value="+" type="submit">
<?php function dodaj01()
{
$fp01 = fopen("licznik01.txt", "r+");
$count01 = fgets($fp01);
$count01 = $count01 + 1;
ftruncate($fp01, 0);
fseek($fp01, 0);
fputs($fp01, $count01);
fclose($fp01);
header("Refresh:0");
}
if(array_key_exists('plus01',$_POST)){
dodaj01();
}
?>
</form>
</td>
    </tr>
    <tr>
      <td align="right" valign="middle"></td>
      <td align="right" valign="middle"></td>
      <td align="right" valign="middle">0,200</td>
<td align="right" valign="middle">
<?php $fp02 = fopen("licznik02.txt", "r+");
$count02 = fgets($fp02);
fclose($fp02);
echo $count02;
?>
</td>
<td align="center" valign="middle">
<form method="post">
<input class="czerwony" name="minus02" id="minus02" value="-" type="submit">
<?php function odejmij02()
{
$fp02 = fopen("licznik02.txt", "r+");
$count02 = fgets($fp02);
$count02 = $count02 - 1;
ftruncate($fp02, 0);
fseek($fp02, 0);
fputs($fp02, $count02);
fclose($fp02);
header("Refresh:0");
}
if(array_key_exists('minus02',$_POST)){
odejmij02();
}
?>
<input class="zielony" name="plus02" id="plus02" value="+" type="submit">
<?php function dodaj02()
{
$fp02 = fopen("licznik02.txt", "r+");
$count02 = fgets($fp02);
$count02 = $count02 + 1;
ftruncate($fp02, 0);
fseek($fp02, 0);
fputs($fp02, $count02);
fclose($fp02);
header("Refresh:0");
}
if(array_key_exists('plus02',$_POST)){
dodaj02();
}
?>
</form>
</td>
    </tr>
    <tr>
      <td align="right" valign="middle"></td>
      <td align="right" valign="middle"></td>
      <td align="right" valign="middle">0,250</td>
<td align="right" valign="middle">
<?php $fp03 = fopen("licznik03.txt", "r+");
$count03 = fgets($fp03);
fclose($fp03);
echo $count03;
?>
</td>
<td align="center" valign="middle">
<form method="post">
<input class="czerwony" name="minus03" id="minus03" value="-" type="submit">
<?php function odejmij03()
{
$fp03 = fopen("licznik03.txt", "r+");
$count03 = fgets($fp03);
$count03 = $count03 - 1;
ftruncate($fp03, 0);
fseek($fp03, 0);
fputs($fp03, $count03);
fclose($fp03);
header("Refresh:0");
}
if(array_key_exists('minus03',$_POST)){
odejmij03();
}
?>
<input class="zielony" name="plus03" id="plus03" value="+" type="submit">
<?php function dodaj03()
{
$fp03 = fopen("licznik03.txt", "r+");
$count03 = fgets($fp03);
$count03 = $count03 + 1;
ftruncate($fp03, 0);
fseek($fp03, 0);
fputs($fp03, $count03);
fclose($fp03);
header("Refresh:0");
}
if(array_key_exists('plus03',$_POST)){
dodaj03();
}
?>
</form>
</td>
    </tr>
    <tr>
      <td align="right" valign="middle"></td>
      <td align="right" valign="middle"></td>
      <td align="right" valign="middle">0,333</td>
<td align="right" valign="middle">
<?php $fp04 = fopen("licznik04.txt", "r+");
$count04 = fgets($fp04);
fclose($fp04);
echo $count04;
?>
</td>
<td align="center" valign="middle">
<form method="post">
<input class="czerwony" name="minus04" id="minus04" value="-" type="submit">
<?php function odejmij04()
{
$fp04 = fopen("licznik04.txt", "r+");
$count04 = fgets($fp04);
$count04 = $count04 - 1;
ftruncate($fp04, 0);
fseek($fp04, 0);
fputs($fp04, $count04);
fclose($fp04);
header("Refresh:0");
}
if(array_key_exists('minus04',$_POST)){
odejmij04();
}
?>
<input class="zielony" name="plus04" id="plus04" value="+" type="submit">
<?php function dodaj04()
{
$fp04 = fopen("licznik04.txt", "r+");
$count04 = fgets($fp04);
$count04 = $count04 + 1;
ftruncate($fp04, 0);
fseek($fp04, 0);
fputs($fp04, $count04);
fclose($fp04);
header("Refresh:0");
}
if(array_key_exists('plus04',$_POST)){
dodaj04();
}
?>
</form>
</td>
    </tr>
    <tr>
      <td align="right" valign="middle">Papier półpołysk klej mocny</td>
      <td align="right" valign="middle">DIG UV INKJET MC80 FSC S2045N-BG40BR </td>
      <td align="right" valign="middle">0,166</td>
<td align="right" valign="middle">
<?php $fp05 = fopen("licznik05.txt", "r+");
$count05 = fgets($fp05);
fclose($fp05);
echo $count05;
?>
</td>
<td align="center" valign="middle">
<form method="post">
<input class="czerwony" name="minus05" id="minus05" value="-" type="submit">
<?php function odejmij05()
{
$fp05 = fopen("licznik05.txt", "r+");
$count05 = fgets($fp05);
$count05 = $count05 - 1;
ftruncate($fp05, 0);
fseek($fp05, 0);
fputs($fp05, $count05);
fclose($fp05);
header("Refresh:0");
}
if(array_key_exists('minus05',$_POST)){
odejmij05();
}
?>
<input class="zielony" name="plus05" id="plus05" value="+" type="submit">
<?php function dodaj05()
{
$fp05 = fopen("licznik05.txt", "r+");
$count05 = fgets($fp05);
$count05 = $count05 + 1;
ftruncate($fp05, 0);
fseek($fp05, 0);
fputs($fp05, $count05);
fclose($fp05);
header("Refresh:0");
}
if(array_key_exists('plus05',$_POST)){
dodaj05();
}
?>
</form>
</td>
    </tr>

"10.php"

    <tr>
      <td align="right" valign="middle"></td>
      <td align="right" valign="middle"></td>
      <td align="right" valign="middle">0,250</td>
<td align="right" valign="middle">
<?php $fp06 = fopen("licznik06.txt", "r+");
$count06 = fgets($fp06);
fclose($fp06);
echo $count06;
?>
</td>
<td align="center" valign="middle">
<form method="post">
<input class="czerwony" name="minus06" id="minus06" value="-" type="submit">
<?php function odejmij06()
{
$fp06 = fopen("licznik06.txt", "r+");
$count06 = fgets($fp06);
$count06 = $count06 - 1;
ftruncate($fp06, 0);
fseek($fp06, 0);
fputs($fp06, $count06);
fclose($fp06);
header("Refresh:0");
}
if(array_key_exists('minus06',$_POST)){
odejmij06();
}
?>
<input class="zielony" name="plus06" id="plus06" value="+" type="submit">
<?php function dodaj06()
{
$fp06 = fopen("licznik06.txt", "r+");
$count06 = fgets($fp06);
$count06 = $count06 + 1;
ftruncate($fp06, 0);
fseek($fp06, 0);
fputs($fp06, $count06);
fclose($fp06);
header("Refresh:0");
}
if(array_key_exists('plus06',$_POST)){
dodaj06();
}
?>
</form>
</td>
    </tr>
    <tr>
      <td align="right" valign="middle"></td>
      <td align="right" valign="middle"></td>
      <td align="right" valign="middle">0,250</td>
<td align="right" valign="middle">
<?php $fp07 = fopen("licznik07.txt", "r+");
$count07 = fgets($fp07);
fclose($fp07);
echo $count07;
?>
</td>
<td align="center" valign="middle">
<form method="post">
<input class="czerwony" name="minus07" id="minus07" value="-" type="submit">
<?php function odejmij07()
{
$fp07 = fopen("licznik07.txt", "r+");
$count07 = fgets($fp07);
$count07 = $count07 - 1;
ftruncate($fp07, 0);
fseek($fp07, 0);
fputs($fp07, $count07);
fclose($fp07);
header("Refresh:0");
}
if(array_key_exists('minus07',$_POST)){
odejmij07();
}
?>
<input class="zielony" name="plus07" id="plus07" value="+" type="submit">
<?php function dodaj07()
{
$fp07 = fopen("licznik07.txt", "r+");
$count07 = fgets($fp07);
$count07 = $count07 + 1;
ftruncate($fp07, 0);
fseek($fp07, 0);
fputs($fp07, $count07);
fclose($fp07);
header("Refresh:0");
}
if(array_key_exists('plus07',$_POST)){
dodaj07();
}
?>
</form>
</td>
    </tr>
    <tr>
      <td align="right" valign="middle">Papier półpołysk klej odlepny</td>
      <td align="right" valign="middle">DIG INDI MC80 TOP FSC R5000N-BG40BR </td>
      <td align="right" valign="middle">0,166</td>
<td align="right" valign="middle">
<?php $fp08 = fopen("licznik08.txt", "r+");
$count08 = fgets($fp08);
fclose($fp08);
echo $count08;
?>
</td>
<td align="center" valign="middle">
<form method="post">
<input class="czerwony" name="minus08" id="minus08" value="-" type="submit">
<?php function odejmij08()
{
$fp08 = fopen("licznik08.txt", "r+");
$count08 = fgets($fp08);
$count08 = $count08 - 1;
ftruncate($fp08, 0);
fseek($fp08, 0);
fputs($fp08, $count08);
fclose($fp08);
header("Refresh:0");
}
if(array_key_exists('minus08',$_POST)){
odejmij08();
}
?>
<input class="zielony" name="plus08" id="plus08" value="+" type="submit">
<?php function dodaj08()
{
$fp08 = fopen("licznik08.txt", "r+");
$count08 = fgets($fp08);
$count08 = $count08 + 1;
ftruncate($fp08, 0);
fseek($fp08, 0);
fputs($fp08, $count08);
fclose($fp08);
header("Refresh:0");
}
if(array_key_exists('plus08',$_POST)){
dodaj08();
}
?>
</form>
</td>
    </tr>
    <tr>
      <td align="right" valign="middle"></td>
      <td align="right" valign="middle"></td>
      <td align="right" valign="middle">0,250</td>
<td align="right" valign="middle">
<?php $fp09 = fopen("licznik09.txt", "r+");
$count09 = fgets($fp09);
fclose($fp09);
echo $count09;
?>
</td>
<td align="center" valign="middle">
<form method="post">
<input class="czerwony" name="minus09" id="minus09" value="-" type="submit">
<?php function odejmij09()
{
$fp09 = fopen("licznik09.txt", "r+");
$count09 = fgets($fp09);
$count09 = $count09 - 1;
ftruncate($fp09, 0);
fseek($fp09, 0);
fputs($fp09, $count09);
fclose($fp09);
header("Refresh:0");
}
if(array_key_exists('minus09',$_POST)){
odejmij09();
}
?>
<input class="zielony" name="plus09" id="plus09" value="+" type="submit">
<?php function dodaj09()
{
$fp09 = fopen("licznik09.txt", "r+");
$count09 = fgets($fp09);
$count09 = $count09 + 1;
ftruncate($fp09, 0);
fseek($fp09, 0);
fputs($fp09, $count09);
fclose($fp09);
header("Refresh:0");
}
if(array_key_exists('plus09',$_POST)){
dodaj09();
}
?>
</form>
</td>
    </tr>
    <tr>
      <td align="right" valign="middle"></td>
      <td align="right" valign="middle"></td>
      <td align="right" valign="middle">0,333</td>
<td align="right" valign="middle">
<?php $fp10 = fopen("licznik10.txt", "r+");
$count10 = fgets($fp10);
fclose($fp10);
echo $count10;
?>
</td>
<td align="center" valign="middle">
<form method="post">
<input class="czerwony" name="minus10" id="minus10" value="-" type="submit">
<?php function odejmij10()
{
$fp10 = fopen("licznik10.txt", "r+");
$count10 = fgets($fp10);
$count10 = $count10 - 1;
ftruncate($fp10, 0);
fseek($fp10, 0);
fputs($fp10, $count10);
fclose($fp10);
header("Refresh:0");
}
if(array_key_exists('minus10',$_POST)){
odejmij10();
}
?>
<input class="zielony" name="plus10" id="plus10" value="+" type="submit">
<?php function dodaj10()
{
$fp10 = fopen("licznik10.txt", "r+");
$count10 = fgets($fp10);
$count10 = $count10 + 1;
ftruncate($fp10, 0);
fseek($fp10, 0);
fputs($fp10, $count10);
fclose($fp10);
header("Refresh:0");
}
if(array_key_exists('plus10',$_POST)){
dodaj10();
}
?>
</form>
</td>
    </tr>

"main.php"

<!DOCTYPE html">
<html><head>
<meta content="text/html; charset=ISO-8859-1" http-equiv="content-type">
<link rel="stylesheet" href="style.css">
<title>Etykiety - Stan magazynowy</title>
</head>
<body>
<div style="text-align: center;">
<h1>Etykiety - Stan magazynowy</h1>

<form method="post">
<h2><input class="odswiez" name="odswiez" id="odswiez" value="ODŚWIEŻ DANE" type="submit"></h2>
<?php function odswiez()
{
header("Refresh:0");
}
if(array_key_exists('odswiez',$_POST)){
odswiez();
}
?>
</form>

<table align=center style="text-align: left; width: 750px;" cellpadding="2" cellspacing="2">
  <tbody>
    <tr>
      <th align="right" valign="middle" height="35px">Nazwa Materiału</th>
      <th align="right" valign="middle">Kod producenta</th>
      <th align="right" valign="middle">Szerokość</th>
      <th align="right" valign="middle">Stan</th>
      <th align="center" valign="middle">Akcje</th>
    </tr>
<?php include '05.php';?>
<?php include '10.php';?>
  </tbody>
</table>

<form method="post">
<h2><input class="odswiez" name="odswiez2" id="odswiez2" value="ODŚWIEŻ DANE" type="submit"></h2>
<?php function odswiez2()
{
header("Refresh:0");
}
if(array_key_exists('odswiez2',$_POST)){
odswiez2();
}
?>
</form>

</div>
</body></html>

Any help is a great help :)

EDIT: Instead of including those files, I've just copy/pasted their code into "main.php" and it is exactly the same. Separatly pasted works fine, pasted together give the same error...

baur
  • 13
  • 4

1 Answers1

1

Long story short
There must not be sent anything even a white space till all headers are sent.

The issue
In your case the script sends plenty of html code, text, etc. and then suddenly it tries to modify the headers with calls like header("Refresh:0"); and this is wrong.

You should send all the headers before the page content is sent to the browser.

Solution:
1. sent all headers at the beginning of the script and then continue with the content
OR
2. use Output Buffering to catch all the output and flush it after all modification of headers are done.

BUT do not mix output and headers together.

Proof of concept
You can comment all the lines with header("Refresh:0"); This should suppress the headers already sent php errors ;)

ino
  • 2,345
  • 1
  • 15
  • 27
  • Thank You for response! I get all that, and I can use any other way of refreshing page instead of "header", but I'm curious, why is it, that this code works for 7 table rows, and no more (all rows are just copy/paste with changed numbers (01 -> 02 -> 03 ... and so on)... If my code for table row was incorect, it would crash in firt and every other row, but instead it's working fine for 7 rows :) – baur Mar 22 '18 at 12:15
  • As I wrote, there might be single white space anywhere that is sent before any new header that could case the header integrity. I am not going to investigate your code it so deeply to find it out. You can run it in debugger to see what is going on there before first header is sent. Btw, if you are happy with answer/solution above, please accept it to let other user know that it works for you. – ino Mar 22 '18 at 12:26
  • And as always I was wrong. Xdebug shows that error even in first table row. So error was all the time, but without Xdebug it just (for some reason) didn't show until row 8... thank You for Your answer! – baur Mar 22 '18 at 13:31