Hello,

I need some help sorting out this error:

Warning: Cannot modify header information - headers already sent by (output started at /Applications/MAMP/htdocs/new/Archive/phplogin/includes/main/header.php:3) in /Applications/MAMP/htdocs/new/Archive/phplogin/register.php on line 71

I've included the code below, can someone please help me figure this out. I've been working om this issue for 4 hours now and I read on these forums that this is an issue with white spaces, but open this with multiple editors and there seems to be no white spaces.

Any help is much appreciated as always.

drjay

<!--includes/main/header.php-->

<!doctype html>
    <html>
    <?php include('includes/head.php'); ?>
    <body>
    <?php include('includes/header.php'); ?>
    <div id="container">
    <?php include('includes/aside.php'); ?>

<!--includes/head.php-->

<head>
    <title>Title</title>
    <meta charset="UTF-8">
    <link rel="stylesheet" href="css/style.css">
    </head>

<!--includes/register.php-->

<?php
    include('core/init.php');
    include('includes/main/header.php');

if (empty($_POST) === false) {
    $required_fields = array('username', 'password', 'retype_password', 'firstname', 'email');
    foreach ($_POST as $key => $value) {
        if (empty($value) && in_array($key, $required_fields) === true) {
            $errors[] = 'Required fields missing, fields marked with the asterisks are required.';
            break 1;
        }
    }

    if (empty($errors) === true) {
        if (userExists($_POST['username']) === true) {
            $errors[] = 'The username \'' . htmlentities($_POST['username']) . '\' is taken, please try again.';
        }

        if (strlen($_POST['username']) < 4 || strlen($_POST['username']) > 32) {
            $errors[] = 'The username must be between 4-32 characters.';
        }

        if (strlen($_POST['firstname']) > 64) {
            $errors[] = 'The first name cannot be more than 64 characters long.';
        }

        if (strlen($_POST['lastname']) > 64) {
            $errors[] = 'The last name cannot be more than 64 characters long.';
        }

        if (preg_match("/\\s/", $_POST['username']) == true) {
            $errors[] = 'Username cannot contain spaces.';
        }

        if (strlen($_POST['password']) < 8 || strlen($_POST['password']) > 32) {
            $errors[] = 'The password must be between 8-32 characters.';
        }

        if ($_POST['password'] !== $_POST['retype_password']) {
            $errors[] = 'The passwords do not match, please try again.';
        }

        if (filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) === false) {
            $errors[] = 'Invalid email, please try again.';
        }

        if (emailExists($_POST['email']) === true) {
            $errors[] = 'The email, \'' . htmlentities($_POST['email']) . '\' is already in use, please try again.';
        }

        if (strlen($_POST['email']) > 254) {
            $errors[] = 'Please check your email address, it\'s more than 254 character.';
        }
    }
}
?>
<h1>Register</h1>
<?php
if (isset($_GET['success']) && empty($_GET['success'])) {
    echo 'Your account has been successfully created!';
} else {
    if (empty($_POST) === false && empty($errors) === true) {
        $registration_data = array(
            'username' => $_POST['username'],
            'password' => $_POST['password'],
            'firstname' => $_POST['firstname'],
            'lastname' => $_POST['lastname'],
            'email' => $_POST['email']
        );
        if (addUser($registration_data) === true) {
            header('Location: register.php?success');
            exit();
        } else {
            $errors[] = 'Error occured while registration, please try again.';
            echo outputErrors($errors);
        }
    } else if (empty($errors) === false) {
        echo outputErrors($errors);
    }
?>
<form action="register.php" method="post">
    <ul id="login">
        <li>
            Username:<br>
            <input type="text" name="username">
        </li>
        <li>
            Password:<br>
            <input type="password" name="password">
        </li>
        <li>
            Re-type Password:<br>
            <input type="password" name="retype_password">
        </li>
        <li>
            First Name:<br>
            <input type="text" name="firstname">
        </li>
        <li>
            Last Name:<br>
            <input type="text" name="lastname">
        </li>
        <li>
            Email:<br>
            <input type="text" name="email">
        </li>
        <li>
            <input type="submit" value="Register">
        </li>
    </ul>
</form>
<?php 
}
include('includes/main/footer.php'); 
?>

Recommended Answers

All 3 Replies

Member Avatar for diafol

You can't redirecct after output to the screen (via php or html). Place all your php in an include file or before the doctype declaration. If your php creates output, save it to a variable and then echo it out in the appropriate place in the html.

There isn't any echo before header(). This peice of code:

echo 'Your account has been successfully created!';

should not run because of the if condition:

if (isset($_GET['success']) && empty($_GET['success'])) {
   echo 'Your account has been successfully created!';
} else {

Do you think this is a problem with the code or white spaces?

Member Avatar for diafol

Doesn't the header.php page with all the html in it get included first?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.