简体   繁体   中英

Getting “Cannot modify header information” from login page

This is probably a duplicate but i am having this issue on login when running the following code

<?php
include('includes/functions.php');

if(isset($_POST['login'])) {
    if(isset($_POST['username'])) {
        if(isset($_POST['password'])) {
            $username = $_POST['username'];
            $query = mysql_query("SELECT * FROM users WHERE Username = '$username'") or die(mysql_error());
            $user = mysql_fetch_array($query);

            if(md5($_POST['password']) == $user['password']); {
                echo 'Login successful';
                $_SESSION['user'] = $user['FullName'];
                header("Location:index.php");
           } 
        } else {
            echo "Please check your password!";
            include('login.php');
        }
    } else {
        echo "Please check your Username!";
        include('login.php');
    }
} else {
    echo "Please check you filled out the login form!";
    include('login.php');
  }
?>

So when username and password are entered i get this output in browser

Login successful
Warning: Cannot modify header information - headers already sent by (output started at /home/site/public_html/admin/dologin.php:12) in /home/site/public_html/admin/dologin.php on line 14

All help will be greatly appreciated

        if(md5($_POST['password']) == $user['password']); {
                                                        ^
            echo 'Login successful';
            $_SESSION['user'] = $user['FullName'];
            header("Location:index.php");
        }

There are 2 issues. That ; inside the if statement should not be there, and then that echo should also go as already mentioned by other answers. Removing the echo should fix that error but your if is messed up because of ; which then causes the header not to work.

Side note: How can I prevent SQL injection in PHP?

header(<..>)之前应该没有输出,因此您应该摆脱它(使用echo删除行)。

USe :-

<script>location.href - "index.php"; </script>

instead of

header("Location:index.php");

add ob_start(); at the beginning of file..and remove spaces before and after php tags..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM