返回验证无效登录警告php脚本

时间:2015-03-31 15:31:50

标签: php mysql

我已经创建了一个成功的登录表单,该表单连接到数据库以确定登录是否正确。但我想更新这个,以便如果输入错误的用户名或密码,他们将收到一条错误消息。我只是不确定如何将其实现到我现有的代码中?...

我的用户登录页面:

<form action="../login.php" method="post">
<label for="login-username"><i class="icon-user"></i> <b>Username</b>    </label><br/>
<input class="form-control" type="text" name="username">
<br/>
<label for="login-password"><i class="icon-lock"></i> <b>Password</b>    </label> <br/>
<input class="form-control" type="password" name="password">
<br/>
<button type="submit" class="btn pull-right">Login</button>
</form>

<?php
if (isset($_SESSION['username'])){
 if($_SESSION['logged_in'] = 1){
echo ('Logged in as:  '. $_SESSION['username'].'    '.$_SESSION['surname']).'<br><a href="logout.php">Log out</a>';
}
}
?>

并将login.php发布到:

<?php

session_start();

$servername = "localhost";
$username = "root";
$password = "";
$dbname = "gpdb";

$conn = new mysqli($servername, $username, $password, $dbname);

if ($conn->connect_error) {
die("connection failed: " . $conn->connect_error);
 }
 //echo "connection successful";

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


$sql = "SELECT * FROM patients where Username ='$username' and Password ='$password'";
$result = $conn->query($sql);
$admin_user = 'admin';
$admin_password = 'admin1';

if ($result->num_rows > 0) {
if ($username === $admin_user || $password === $admin_password ){
foreach($result as $row) {
//echo "PatientID " .$row["PatientID"]."<br>". "First name and Last   name: " . $row["Firstname"]. " ".$row["Surname"]. "<br/>";
$_SESSION['id'] = $row["PatientID"];
$_SESSION['username'] = $row["Firstname"];
$_SESSION['surname'] = $row["Surname"];
$_SESSION['logged_in'] = 2;
header("location: http://localhost/index.php");
die;
}
}else{ 
foreach($result as $row) {
$_SESSION['id'] = $row["PatientID"];
$_SESSION['username'] = $row["Firstname"];
$_SESSION['surname'] = $row["Surname"];
$_SESSION['logged_in'] = 1;

header("location: http://localhost/index.php");
die;
}
}
}else{
$_SESSION['logged_in'] = 0;
header("location: http://localhost/user.php");
die;
}

?>

1 个答案:

答案 0 :(得分:1)

<?php

if ($result->num_rows > 0){
    header("location: http://localhost/index.php");

}else{
    echo "Wrong Username or Password <br />".
    '<a href="http://localhost/user.php">Go back...</a>';
}
?>

您还可以创建login_failure.php页面,并在else部分将用户重定向到该页面。或另一种方法是传递失败消息的值

header("location: http://localhost/user.php?msg = 1");

并在登录框顶部显示消息。获得&#39; msg&#39;的价值在user.php页面中,如果条件显示消息则应用。

<div><?php
$msg = $_GET['msg'];
if (isset($msg)) { echo "Wrong username/password"; } ?> </div>
    <form action="../login.php" method="post">
    <label for="login-username"><i class="icon-user"></i> <b>Username</b>    </label><br/>
    <input class="form-control" type="text" name="username">
    <br/>
    <label for="login-password"><i class="icon-lock"></i> <b>Password</b>    </label> <br/>
    <input class="form-control" type="password" name="password">
    <br/>
    <button type="submit" class="btn pull-right">Login</button>
    </form>