Skip to content Skip to sidebar Skip to footer

Rearranging Php Page So Header Is Before Html

My header is not redirecting to the correct page because I believe I have HTML elements before it. I am new to PHP and need help rearranging my page so this isn't the case. If I re

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.

<?phpinclude ("storescripts/init.php");
protect_page();
include ("includes/overall/head.php");

if (empty($_POST) ===  false){
    $required_fields = array('current_password','password','password_again');
    foreach ($_POSTas$key=>$value) {
        if (empty($value) && in_array($key, $required_fields) == true) {
            $errors[] = 'Fields marked with an asterisk are required';
            break1;
        }
    }
    if ($_POST['current_password'] === $member_data['mem_password']) {
        if(trim($_POST['password']) !== trim($_POST['password_again'])){
            $errors[] = 'Your new passwords do not match';
        } elseif (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><?phpinclude ("includes/overall/template_header.php");?><divid="mainDivShort"><h1>Change Password</h1><divid="divBreak"></div><?phpinclude ("includes/overall/column_left.php");?><divid="middleContent"><?phpif (isset($_GET['success']) && isset($_GET['success'])) {
            echo'You have been registered successfully';
        } else {
if (empty($errors) === false) {
echo output_errors($errors);
}?><formaction=""method="post"><ul><li>Current Password*: <br><inputtype="password"name="current_password"></li><li>New Password*: <br><inputtype="password"name="password"></li><li>Repeat New Password*: <br><inputtype="password"name="password_again"></li><li><inputtype="submit"value="Change"></li></ul></form><?php }?></div><?phpinclude ("includes/overall/column_right.php");?></div><?phpinclude ("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 redirectionheader('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"