Rearranging PHP Page So Header Is Before HTML
Solution 1:
Your header needs to go before any output. That includes your includes. include ("storescripts/init.php");
, include ("includes/overall/head.php");
,include ("includes/overall/template_header.php");
any output will stop the header.
Solution 2:
This arrangement ought to work. We just move the change password if()
above any of your document. Note the die()
command which tells PHP to stop processing. No need to execute the rest of the script when we've instructed the browser to carry on someplace else.
As Dan points out below, you'll want to make sure your include()
d files do not contain any output.
<?php
include ("storescripts/init.php");
protect_page();
include ("includes/overall/head.php");
if (empty($_POST) === false){
$required_fields = array('current_password','password','password_again');
foreach ($_POST as $key=>$value) {
if (empty($value) && in_array($key, $required_fields) == true) {
$errors[] = 'Fields marked with an asterisk are required';
break 1;
}
}
if ($_POST['current_password'] === $member_data['mem_password']) {
if(trim($_POST['password']) !== trim($_POST['password_again'])){
$errors[] = 'Your new passwords do not match';
} else if (strlen($_POST['password']) <6) {
$errors[] = 'Your password must be at least 6 characters';
}
} else {
$errors[] = 'Your current password is incorrect';
}
}
if(empty($_POST) === false && empty($errors) === true) {
change_password($session_mem_id, $_POST['password']);
header('Location: changepassword.php?success=1');
die();
}
?><!DOCTYPE html>
<html>
<body>
<?php include ("includes/overall/template_header.php");?>
<div id="mainDivShort">
<h1>Change Password</h1>
<div id="divBreak"></div>
<?php include ("includes/overall/column_left.php");?>
<div id="middleContent">
<?php if (isset($_GET['success']) && isset($_GET['success'])) {
echo 'You have been registered successfully';
} else {
if (empty($errors) === false) {
echo output_errors($errors);
}?>
<form action="" method="post">
<ul>
<li>Current Password*: <br> <input type="password"
name="current_password">
</li>
<li>New Password*: <br> <input type="password" name="password">
</li>
<li>Repeat New Password*: <br> <input type="password"
name="password_again">
</li>
<li><input type="submit" value="Change">
</li>
</ul>
</form>
<?php }?>
</div>
<?php include ("includes/overall/column_right.php");?>
</div>
<?php include ("includes/overall/template_footer.php");?>
</body>
</html>
Solution 3:
This is not an answer, maybe useful informations if you dont know this.
To make use header()
function - you have to execute it before any echo, print_r etc.
Other words - "product of" header();
function must be first data sent to browser.
Solution 4:
You could use output buffering.
In PHP, headers must be sent before the content. So once you output anything (includes echo
, print
, etc, and anything outside the <?php ?>
blocks including whitespace), then the headers cannot be modified.
However: What you can do is turn on output buffering, which will sent all output into a temporary buffer. That buffer will not be output until you tell it to.
The basic pattern is:
ob_start(); //Turn on output buffering at the
// beginning of your script
echo "Hello world!"; //Here would be your HTML document
if ($redirect) //This would be your condition for redirection
header('Location: otherpage.php');
else
ob_flush(); //At the very end, send the buffer to the browser
There are a number of options for ending the buffering. In this case I simply flushed the output by using ob_flush()
, but you can check out the PHP documentation for various other options.
Post a Comment for "Rearranging PHP Page So Header Is Before HTML"