-3

I get this error: Warning: Cannot modify header information - headers already sent by (output started at /Applications/XAMPP/xamppfiles/htdocs/recover.php:11) in /Applications/XAMPP/xamppfiles/htdocs/recover.php on line 22

This is my code so that you can take a look:

<?php

include 'core/init.php';
logged_in_redirect();

?>


<h1> Recover </h1>

<?php
if (isset($_GET['success']) === true && empty($_GET['success']) === true) {
?>
    <p>thanks we have emailed you</p>
<?php
} else {
    $mode_allowed = array('username', 'password');
        if(isset($_GET['mode']) === true && in_array($_GET['mode'], $mode_allowed) === true) {
            if(isset($_POST['email']) === true && empty($_POST['email']) === false){
                if (email_exists($_POST['email']) === true) {
                    recover($_GET['mode'], $_POST['email']);
                    header('Location: recover.php?success');
                    exit();
                } else {
                    echo 'we cant find that email in our database';
                }
            }

?>
    <form action="" method="post">
        Please enter your email adress:<br>
        <input type="text" name="email"><br>
        <input type="submit" value="Recover!">


    </form>

<?php
    } else {
        header('Location: index.php');
        exit();
    }
}

?>

ANY IDEA ON HOW CAN I FIX THIS? Thanks in advance

1 Answers1

1

You cannot changer header information, redirection in your case, after anything has been output by PHP.

Removing <h1> Recover </h1> will fix the error, but you should rethink your code so that header('Location: recover.php?success'); appears before any HTML output.

Pier-Luc Gendreau
  • 13,553
  • 4
  • 58
  • 69