-2

Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\d\login.php:15) in C:\xampp\htdocs\d\login.php on line 41
I am Getting This Error While Logging in Form What is Problem On Line 41?

<!doctype html>
<html>
<head>
<title>Login</title>
</head>
<body>

<p><a href="register.php">Register</a> | <a href="login.php">Login</a></p>
<h3>Login Form</h3>
<form action="" method="POST">
    Username: <input type="text" name="user"><br />
    Password: <input type="password" name="pass"><br /> 
    <input type="submit" value="Login" name="submit" />
</form>
<?php
if(isset($_POST["submit"])){
    if(!empty($_POST['user']) && !empty($_POST['pass'])) {
        $user=$_POST['user'];
        $pass=$_POST['pass'];

        $con=mysql_connect('localhost','yoyo','glappyhunt') or die(mysql_error());
        mysql_select_db('user_registration') or die("cannot select DB");

        $query=mysql_query("SELECT * FROM login WHERE username='".$user."' AND password='".$pass."'");
        $numrows=mysql_num_rows($query);
        
        if($numrows!=0) {
            while($row=mysql_fetch_assoc($query)) {
                $dbusername=$row['username'];
                $dbpassword=$row['password'];
            }

            if($user == $dbusername && $pass == $dbpassword0 {
                (ob_start());
                $_SESSION['sess_user']=$user;

                /* Redirect browser */
                header("Location: member.php");
            }
        } else {
            echo "Invalid username or password!";
        }
    } else {
        echo "All fields are required!";
    }
}
?>

</body>
</html>
pavel
  • 26,538
  • 10
  • 45
  • 61
Sukhchain Singh
  • 75
  • 1
  • 10
  • 1
    possible duplicate of [How to fix "Headers already sent" error in PHP](http://stackoverflow.com/questions/8028957/how-to-fix-headers-already-sent-error-in-php) – Rizier123 Mar 02 '15 at 09:27

3 Answers3

0

Header has to be before all output, move the PHP script to the top of page.

<?php
   ...
?>

<!doctype>
<html>
    ...
pavel
  • 26,538
  • 10
  • 45
  • 61
0

Put the if condition on top of the page.

You should not send any output before sending header not even html. In your code html tags are sent as output before header is called. As per information on php.net for headers.

Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP.

Generally header contains information of the content which is to be sent, like content-type, size, resource status. If you send raw output first and then send headers, headers have no meaning thus the error.

Sachin Joshi
  • 119
  • 5
0

In addition to panther's reply, there are some situations where we have to do redirection after the html is displayed. All though, this might not be considered as a good practice (and I will load the view after everything is done anyway), but we can hack it a little with javascript redirect.

For this example, we could also do the following:

 if($user == $dbusername && $pass == $dbpassword0 {

        $_SESSION['sess_user']=$user;
        /* Redirect browser */
         echo '<script>document.location.href="members.php";</script>';
         die();
           }

Again, this is not really the best practice, but may come handy sometimes.

Gogol
  • 3,033
  • 4
  • 28
  • 57