2015-10-17 112 views
-2

我有两页。 Index.php和我的Login.php。在同一页面上显示PHP登录错误?

我正在使用下拉菜单表单登录 - 当用户按下'登录'时,数据被发送到Login.php。当密码/用户名正确时(它们被发送到仪表板)都可以。

但是我怎样才能在窗体中显示错误呢?目前,当用户名/密码错误时,它会将您发送到新的空白页面。我想让错误出现在表单上。

我的形式:

<div class="form-group"> 
    <label class="sr-only" for="username">Username</label> 
    <input type="text" class="form-control" name="user_name" placeholder="Username" required> 
</div> 
<div class="form-group"> 
    <label class="sr-only" for="password">Password</label> 
    <input type="password" class="form-control" name="user_password" placeholder="Password" required> 
</div> 
<div class="checkbox"> 
    <label> 
    <input type="checkbox"> Remember me 
    </label> 
</div> 
<div class="form-group"> 
    <button type="submit" class="btn btn-success btn-block">Sign in</button> 
</div> 

我的login.php:

error_reporting(E_ALL); 

session_start(); 
$con = mysqli_connect("localhost", "root", "", "battlesql"); 

if (!$con) { 
    echo "<div>"; 
    echo "Failed to connect to MySQL: ".mysqli_connect_error(); 
    echo "</div>"; 
} else { 
    if (isset($_POST['user_name']) && isset($_POST['user_password'])) { 

     $username = stripslashes($username); 
     $username = mysqli_real_escape_string($con, $_POST['user_name']); 

     $pass = stripslashes($pass); 
     $pass = mysqli_real_escape_string($con, $_POST['user_password']); 

     $pass_hashed = hash('whirlpool', $pass); 

     $select_user = "SELECT * from accounts where name='$username' AND password='$pass_hashed' LIMIT 1"; 
     $query = mysqli_query($con, $select_user); 

     $row = mysqli_num_rows($query); 

     if (!$row) { 
      echo "<div>"; 
      echo "No existing user or wrong password."; 
      echo "</div>"; 
     } else { 
      echo "<div>"; 
      echo "You have been logged in."; 
      echo "</div>"; 
     } 

    } else { 
     echo "MySQL error!"; 
    } 
} 
+1

一些代码,请 – Berriel

+0

如何问问题[最小,完整和可验证示例](http://stackoverflow.com/help/mcve)/ [如何问](http://stackoverflow.com/questions/how-to-ask) –

回答

1

在您的登录。PHP的,而不是附和“没有现有用户或密码错误”,使用此:

if(!$row) 
     { 
      die(header("location:index.php?loginFailed=true&reason=password")); 
     } 

而在你的index.php,你可以生成消息:

<button type="submit" class="btn btn-success btn-block">Sign in</button> 
<?php $reasons = array("password" => "Wrong Username or Password", "blank" => "You have left one or more fields blank."); if ($_GET["loginFailed"]) echo $reasons[$_GET["reason"]]; ?> 
+0

它在我输入密码错误后有效(有没有办法可以使用dropdo wn菜单在此之后打开?打开后我只能看到这条消息)。但是,如果我第一次访问该页面并打开下拉菜单,它会给我一个错误。注意:未定义的索引:loginFailed在第51行的C:\ xampp \ htdocs \ header.php中 – fidtal

+0

你把这行放在哪里?你必须放在你有我提到的回声的地方。更新我的回答以作进一步解释 – MohitC

+0

我在按钮后面放置了回显。这是我第一次访问该页面时发生的。如果我在错误的密码后访问它,那么消息是正确的。 – fidtal

0

你可以达到你想要使用的是什么ajax(asynchronous JavaScript and XML)。使用ajax致电,将登录数据从index.php发送到login.php。阅读回复ajax调用返回并显示错误/成功消息。

您可以使用jquery $.ajax({});函数来实现此模型。如果您在执行$.ajax()功能时遇到问题,那么您可以阅读关于它的所有内容。here

另外,如果你还没有做到这一点,我建议你在发送密码之前给encryptlogin.php

0

有几十种方法可以做到这一点,但我会为您提供一个。

看你的PHP代码,我不能完全确定您的重定向到“仪表板”是的,但说你有一些像这样的代码:

<?php 

//Fetch stuff from the database 
if($fetchedUsername == $username and $fetchedPassword == $password) 
{ 
    header("Location:/dashboard"); 
    return; 
} 

?> 

然后,送他们回登录表单,您只需在else语句中添加一些内容:

<?php 

if(........) 
{ 
    //Redirect to dashboard 
} 
else 
{ 
    header("Location:/login"); 
    return; 
} 

?> 

这会将它们发送回表单。现在你要显示一个错误,所以要做到这一点的方法之一是设置一个GET变量,所以在else语句,改变头:

header("Location:/login/?error=1"); 

这将让你检查的错误代码登录页面。

因此到您的登录页面的代码。您可以使用isset()和内联PHP回声将错误直接打印到表单中。事情是这样的:

<form> 
    //Form stuff 
    <?php if(isset($_GET["error"])):?> 
     <div class="error">Invalid Username or Password</div> 
    <?php endif; ?> 
</form> 

这样,你可以风格div.error,而不用担心显示出来时,没有设置$_GET["error"]一个空的包装。

您也可以使代码呼应使用内联PHP短一点:

<?=(isset($_GET["error"])) ? "Invalid Username or Password" : ""?> 
+0

太棒了建议,但唯一的缺点是,登录表单位于我的index.html上的下拉菜单中,而不是在单独的/登录页面上:l – fidtal