Reputation:
I cannot for the life of me figure out why this code is not working!
For some reason, when I submit the form, the header does not redirect me to ./index?succes#generalSet
. However, it worked in an older version of this page. Using exactly the same code.
Code:
<span>
<?php
if (isset($_GET['success']) === true && empty($_GET['success']) === true) {
echo 'Details have been updated!<br>
Please allow up to 7 minutes for changes to take effect.';
} else {
if (empty($_POST) === false && empty($errors) === true) {
$update_data = array(
'first_name' => $_POST['first_name'],
'last_name' => $_POST['last_name'],
'email' => $_POST['email'],
'description' => $_POST['description']
);
update_user($session_user_id, $update_data);
header('Location: index?success#generalSet');
exit();
} else if (empty($errors) === false) {
echo output_errors($errors);
}
?>
</span>
<form action="" method="post">
<ul style="list-style-type:none;padding-top:50px;margin-left:-40px;">
<li>
First Name*:<br>
<input type="text" name="first_name" value="<?php echo $user_data['first_name']; ?>"><br>
</li>
<li>
Last Name:<br>
<input type="text" name="last_name" value="<?php echo $user_data['last_name']; ?>"><br>
</li>
<li>
Email*:<br>
<input type="text" name="email" value="<?php echo $user_data['email']; ?>"><br>
</li>
<li>
Description:<br>
<textarea type="text" rows="7" cols="42" value="<?php echo $user_data['description']; ?>" name="description"></textarea><br>
</li>
<li>
<input type="submit" class="btn btn-primary" value="Save"><br>
</li>
</ul>
</form>
<?php }?>
Upvotes: 1
Views: 67
Reputation: 11943
The answer as to why this may have worked previously, but is not working now, probably resides in your PHP configuration. PHP has a output_buffering
directive, which can allow PHP to buffer output up to certain length before trying to send this output to the client. This means that if you had this directive high enough at any point prior where the code was working, you would not get the PHP Warning: Cannot modify header information - headers already sent by ... (output started at ...)
error that you are probably getting now. As mentioned in the other answer, you should always try to set your headers before sending any output.
Upvotes: 1
Reputation: 20430
Your header code:
header('Location: index?success#generalSet');
needs to appear before any HTML output. However earlier in the document you have (at least):
<span>
You need to move the header output, and all associated logic, to a place in the file prior to any HTML output being sent. This is because the HTTP spec does not allow headers to be adjusted after content is sent - headers need to be at the start.
To solve this problem it is comment to structure a page thus:
<?php
// Logic here, including redirects
?>
<!DOCTYPE html>
<html>
<!-- content here -->
</html>
If you have previously got this to work, it is possible that PHP stored some of your content in the output buffer before flushing it to the web server. If so, this will have allowed PHP to inject headers into the connection prior to sending the content. However, if you find that this works, I would not recommend you rely upon it: headers should always be sent before content.
Update: a helpful commenter below pointed out this code clause:
if (isset($_GET['success']) === true && empty($_GET['success']) === true) {
echo 'Details have been updated!<br>
Please allow up to 7 minutes for changes to take effect.';
} else {
This counts as HTML output as well, and could well prevent the header from working. In any case, why write to the screen if you intend to redirect immediately anyway? In general you should use the session if you want a message to persist across HTTP operations.
Upvotes: 2