1

I don't understand the problem in my code.I am trying to redirect the user if username and password are correct.

I read from a documentation that "header must be called before any actual output is sent". What kind of output is this referring to? I don't think I'm outputting anything. Here is my code. Thanks in advance.

<?php
require "index-header.html";
?>

<div class="display">
    <?php 
    if(isset($_POST["logout"])) {
        session_unset();
        session_destroy();
    }

    if(isset($_POST["login"])) {
        $name = $_POST["name"];
        $password = $_POST["password"];

        // connect to database
        $db = connect();

        $query = "SELECT admin_name, admin_password FROM login WHERE admin_name = :name AND admin_password = :password";
        $login = $db->prepare($query);
        $login->execute(array(
            ":name" => $name,
            ":password" => $password
        ));

        if($login->rowCount()) {
            // start session
            $_SESSION["admin"] = "Laura";
            header("Location:admin.php");
            exit;
        }
        else {
            echo "<p class='message'>Incorrect username or password</p><br>";
            require "login-form.html";
        }
    }
    else {
        require "login-form.html";
    }
    ?>
</div>
4
  • It mean that you have to put headers at the top of the php file. Even a single white space should not be outputted Commented Feb 17, 2015 at 15:37
  • if you are not using output buffering then you can't send headers once you have rendered html. At the top of every page you would want to turn on output buffering ( ob_start() etc) - build the page and at the end of the page send the buffer to the client / browser using something like ob_end_flush() etc Commented Feb 17, 2015 at 15:38
  • 1
    Move that php code up above the index-header.html and it should work, although I have to say I think your desire throw a bunch of PHP in with your HTML is a bad design choice, but it's your choice to make. Commented Feb 17, 2015 at 15:40
  • put your logic before html put it in starting of the file before including html file. Commented Feb 17, 2015 at 16:12

3 Answers 3

3

Output means any HTML/CSS/JavaScript (or other data) being output in the browser.

There's two problems in your code that would prevent your header change from redirecting:

require "index-header.html";

and

<div class="display">

The better explanation for this in basic terms is that headers can only be modified before any output begins. Every time you visit a web page, headers are sent before the body. Headers can give information like character sets, response codes (like 404), or redirect information. What if you needed to display Arabic characters instead of English? The headers would need to let the browser know how to handle the incoming body (or how you're referring to it: output).

Sign up to request clarification or add additional context in comments.

Comments

2

As mentioned by rick6, before redirect you can not use HTML. You can move your login check before require

<?php
/* check login */
// if admin redirect
// else
// $message = "<p class='message'>Incorrect username or password</p><br>";

require "index-header.html";
echo '<div class="display">';
....
?>

Comments

1

header must be called before any actual output is sent.

But, here, <div> is an input, and you require index-header.html, so header location can't works.

You can use JS :

    function nextpage() {
        document.location.href='nextpage.php';
    }
    nextpage();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.