如何在基本的PHP登录脚本上停止显示为默认值

RSM*_*RSM 0 php forms if-statement login

我正在PHP建立一个非常基本的login script.然而,elseifelse statement显示用户之前默认甚至点击log in.在用户尝试登录之前,他们会受到以下问题的欢迎:

Warning: Cannot modify header information - headers already sent by (output started at /home/madhous3/public_html/dev/admin/index.php:12) in /home/madhous3/public_html/dev/admin/login.php on line 13
Sorry, please try again.
Run Code Online (Sandbox Code Playgroud)

我怎么阻止这个?但是,如果用户正确输入详细信息,则会将其定向到右侧页面.

的index.php

   <?php 
        include("login.php"); 
        ?>
        <h1>Admin Area Login</h1>



        <form method="post" action="login.php">
        Username<input type="text" name="username" />
        Password<input type="text" name="password" />
        <input type="submit" name="log_in" value="Log In" />

        </form>
Run Code Online (Sandbox Code Playgroud)

的login.php

<?php 

$username_inputted = $_POST['username'];
$password_inputted = $_POST['password'];


if($username_inputted == 'admin' && $password_inputted == 'password'){

header("location:login_success.php");


}else{
header("location:index.php");
echo "Sorry, please try again."; 
}



?>
Run Code Online (Sandbox Code Playgroud)

Mar*_*tin 6

尝试从index.php中删除include("login.php").

相反,您应该从login.php重定向回index.php,并带有指示用户输入错误信息的标志(如果登录失败).

的index.php

<?php
    if(isset($_REQUEST['fail'])) {
        echo 'Login failed.';
    }
?>
<h1>Admin Area Login</h1>
<form method="post" action="login.php">
Username<input type="text" name="username" />
Password<input type="text" name="password" />
<input type="submit" name="log_in" value="Log In" />
</form>
Run Code Online (Sandbox Code Playgroud)

的login.php

<?php 

$username_inputted = $_POST['username'];
$password_inputted = $_POST['password'];

if($username_inputted == 'admin' && $password_inputted == 'password'){
    header("location:login_success.php");
} else {
    header("location:index.php?fail=1");
}
?>
Run Code Online (Sandbox Code Playgroud)