Headers already sent error PHP form

Why this form is causing "headers already sent error":
<div class="form-container upload">
<form class="default-form upload" action="upload-game" method="post" enctype="multipart/form-data">
<label for="title">Title: </label>
<input minlength="3" maxlength="40" type="text" name="title" required id="title" autocomplete="off"/>

<label for="description">Description <p style="color:rgb(208, 208, 208)">[optional]</p>:</label>
<textarea maxlength="2000" name="description" rows="4" cols="50" id="description" autocomplete="off"></textarea>

<label for="release_date">Release date: </label>
<input type="date" name="release_date" required id="release_date" autocomplete="off"/>

<label for="embed">Embed <p style="color:rgb(208, 208, 208)">[recommended size: 500x300] [optional]</p>: </label>
<input class="upload-btn" type="file" name="embed" id="embed" autocomplete="off"/>

<label for="browser_file">Playable <p style="color:rgb(208, 208, 208)">[ZIP, should contain an index.html file] [optional]</p>: </label>
<input class="upload-btn" type="file" name="browser_file" id="browser_file" autocomplete="off"/>

<label for="download_file">Downloadable <p style="color:rgb(208, 208, 208)">[should be a ZIP file] [optional]</p>: </label>
<input class="upload-btn" type="file" name="download_file" id="download_file" autocomplete="off"/>

<p style="color: white;">Note: All settings can be changed later.</p>

<button type="submit">Upload</button>
</form>

<div id="error_container" class="error-container"></div>
</div>
<div class="form-container upload">
<form class="default-form upload" action="upload-game" method="post" enctype="multipart/form-data">
<label for="title">Title: </label>
<input minlength="3" maxlength="40" type="text" name="title" required id="title" autocomplete="off"/>

<label for="description">Description <p style="color:rgb(208, 208, 208)">[optional]</p>:</label>
<textarea maxlength="2000" name="description" rows="4" cols="50" id="description" autocomplete="off"></textarea>

<label for="release_date">Release date: </label>
<input type="date" name="release_date" required id="release_date" autocomplete="off"/>

<label for="embed">Embed <p style="color:rgb(208, 208, 208)">[recommended size: 500x300] [optional]</p>: </label>
<input class="upload-btn" type="file" name="embed" id="embed" autocomplete="off"/>

<label for="browser_file">Playable <p style="color:rgb(208, 208, 208)">[ZIP, should contain an index.html file] [optional]</p>: </label>
<input class="upload-btn" type="file" name="browser_file" id="browser_file" autocomplete="off"/>

<label for="download_file">Downloadable <p style="color:rgb(208, 208, 208)">[should be a ZIP file] [optional]</p>: </label>
<input class="upload-btn" type="file" name="download_file" id="download_file" autocomplete="off"/>

<p style="color: white;">Note: All settings can be changed later.</p>

<button type="submit">Upload</button>
</form>

<div id="error_container" class="error-container"></div>
</div>
17 Replies
QWGames
QWGames11mo ago
When this isn't returns any error in the php:
<div class="form-container upload">
<form class="default-form upload" action="upload-game" method="post" enctype="multipart/form-data">
<label for="title">Title: </label>
<input minlength="3" maxlength="40" type="text" name="title" required id="title" autocomplete="off"/>

<label for="description">Description:</label>
<textarea maxlength="2000" name="description" rows="4" cols="50" id="description" autocomplete="off"></textarea>

<label for="release_date">Release date: </label>
<input type="date" name="release_date" required id="release_date" autocomplete="off"/>

<label for="embed">Embed: </label>
<input class="upload-btn" type="file" name="embed" id="embed" autocomplete="off"/>

<label for="browser_file">Playable: </label>
<input class="upload-btn" type="file" name="browser_file" id="browser_file" autocomplete="off"/>

<label for="download_file">Downloadable: </label>
<input class="upload-btn" type="file" name="download_file" id="download_file" autocomplete="off"/>

<button type="submit">Upload</button>
</form>

<div id="error_container" class="error-container"></div>
</div>
<div class="form-container upload">
<form class="default-form upload" action="upload-game" method="post" enctype="multipart/form-data">
<label for="title">Title: </label>
<input minlength="3" maxlength="40" type="text" name="title" required id="title" autocomplete="off"/>

<label for="description">Description:</label>
<textarea maxlength="2000" name="description" rows="4" cols="50" id="description" autocomplete="off"></textarea>

<label for="release_date">Release date: </label>
<input type="date" name="release_date" required id="release_date" autocomplete="off"/>

<label for="embed">Embed: </label>
<input class="upload-btn" type="file" name="embed" id="embed" autocomplete="off"/>

<label for="browser_file">Playable: </label>
<input class="upload-btn" type="file" name="browser_file" id="browser_file" autocomplete="off"/>

<label for="download_file">Downloadable: </label>
<input class="upload-btn" type="file" name="download_file" id="download_file" autocomplete="off"/>

<button type="submit">Upload</button>
</form>

<div id="error_container" class="error-container"></div>
</div>
Jochem
Jochem11mo ago
there is no PHP code here
QWGames
QWGames11mo ago
I'll provide more info in a sec
Jochem
Jochem11mo ago
headers already sent is usually the case if you try to use the header function after you've already sent text to the browser, either outside of <?php ?> or using echo/print
QWGames
QWGames11mo ago
it throws the errors at this place:
if ($_SERVER["REQUEST_METHOD"] === "POST")
{
//Check if the user is logined
if (session_status() === PHP_SESSION_NONE)
{
ini_set('session.cookie_lifetime', 30 * 24 * 60 * 60);
session_start();
}
if ($_SESSION["username"] === null)
{
$error = "Please login to your account to upload a game.";
echo "<script>document.getElementById('error_container').innerHTML = '" . addslashes($error) . "';</script>";
die($error);
}
the error:
if ($_SERVER["REQUEST_METHOD"] === "POST")
{
//Check if the user is logined
if (session_status() === PHP_SESSION_NONE)
{
ini_set('session.cookie_lifetime', 30 * 24 * 60 * 60);
session_start();
}
if ($_SESSION["username"] === null)
{
$error = "Please login to your account to upload a game.";
echo "<script>document.getElementById('error_container').innerHTML = '" . addslashes($error) . "';</script>";
die($error);
}
the error:
html <b>Warning</b>: ini_set(): Session ini settings cannot be changed after headers have already been sent on line <b>131</b><br /> <br /> <b>Warning</b>: session_start(): Session cannot be started after headers have already been sent on line <b>132</b><br /> <br /> <b>Warning</b>: Undefined global variable $_SESSION on line <b>134</b><br /> <br /> <b>Warning</b>: Trying to access array offset on value of type null on line <b>134</b><br /> There is a JS code what checks if the user is logged or not, and it is getting a response from another php about the user status. That contains an echo, but with the first html form is working
Jochem
Jochem11mo ago
where is line 131? is that the ini_set line?
QWGames
QWGames11mo ago
yes
Jochem
Jochem11mo ago
also, why on earth are you using ini_set? Just change php.ini
QWGames
QWGames11mo ago
but thats should not be the problem
Jochem
Jochem11mo ago
It is part of it though
ini_set(): Session ini settings cannot be changed after headers have already been sent
can you share the rest of the file?
QWGames
QWGames11mo ago
sure
Jochem
Jochem11mo ago
PHP can send headers along with the response Apache or nginx sends to the user, but it can only do that before there is any other output. Once there's other output sent, the server will have to end the part of the response where the headers live, and start the part of the response where the body (output of your PHP file) lives. Some things have to be done before the headers are sent, stuff like starting a session or setting an ini directive The file you shared has a ton of HTML and JS before your <?php opening tag. That means that PHP will read all that, and send it to the server as output, which will then by necessity close the phase of the response where it can send headers, and start sending the body basically, if you want to start a session and change ini settings, your file has to start with a php block (<?php through ?>), and those function calls have to be before any echos, prints, or other things that send output to the browser
QWGames
QWGames11mo ago
then should I remove the php from the body and put it to the begining of the file outside the html tag?
Jochem
Jochem11mo ago
that would probably work yeah
QWGames
QWGames11mo ago
alright and its working what an amateur problem thank you for your help I really appreciate it
Jochem
Jochem11mo ago
no worries
Want results from more Discord servers?
Add your server