Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/228.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何停止在基本PHP登录脚本上显示为默认值的else_Php_Forms_Login_If Statement - Fatal编程技术网

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

如何停止在基本PHP登录脚本上显示为默认值的else,php,forms,login,if-statement,Php,Forms,Login,If Statement,我正在使用PHP构建一个非常基本的登录脚本。但是,ifelse语句中的else在用户单击登录之前默认显示。 在用户尝试登录之前,他们会收到以下信息: 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/adm

我正在使用
PHP
构建一个非常基本的
登录脚本。但是,
ifelse语句
中的
else
在用户单击
登录之前默认显示。
在用户尝试登录之前,他们会收到以下信息:

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.
我该怎么阻止这一切?但是,如果用户正确输入了详细信息,则会将其定向到正确的页面

代码

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>
<?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>

管理区登录
用户名
密码
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."; 
}



?>
<?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");
}
?>

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

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

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>
<?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>

管理区登录
用户名
密码
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."; 
}



?>
<?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");
}
?>

试试这个

if (!empty($_POST['username']) && !empty($_POST['password'])) {
    //define input vars
      $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."; 
      }
    }

首先去掉标题('location:login.php')。如果已经开始向浏览器发送任何HTML,则无法发送标题。如果它真的起作用,你会得到一个无休止的重新加载循环

然后:

您可以检查$u POST['submit'],如果它不存在,则不要向他们显示重试消息

好的,现在的情况是,在index.php中,您在开始时包括login.php。当时它从login.php导入所有内容。既然您包含了它,脚本就要运行了

加载index.php页面时,login.php上的脚本启动。它将那些变量$username\u inputed和$password\u inputed定义为null,因为帖子还没有出现。然后if块检查并找到空变量,然后else块激发,因为这些变量不等于预期的登录信息,因为它们是空的

因此,在发布任何内容之前,回声会触发并显示在屏幕上


Nav_Nav的解决方案应该可以很好地工作,因为只有当有人在输入字段中输入了某个内容时才会显示“坏登录”回显,我只想给你一个算法出错原因的简要说明。

重定向后如何才能有
回显
?它从表单到登录脚本,如果用户失败,他们将返回登录页面(index.php)并显示回显。无论如何,这就是希望。将要发生的是
登录尝试
->
登录失败
->
重定向到index.php
而不是
登录尝试
->
登录失败
重定向到index.php
->
回音“对不起,请再试一次。”
耶,我现在意识到了。我现在所做的只是:登录尝试->登录失败->登录失败.php或登录尝试->登录成功.php