在PHP中尝试登录后黑屏

时间:2019-03-11 17:00:05

标签: php

我使用PHP创建了一个登录表单,尽管一旦输入用户名和密码,它就会重定向到我的身份验证页面,该页面变为空白。我尝试输入正确和不正确的详细信息,但是均未收到任何答复,我们将提供任何帮助,我的代码如下:

Login.php

<!DOCTYPE html>
<html lang="en">
  <head>
  <style>



form {
    text-align:center;
}

  </style>

    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>HIS - Login</title>
    <link href="css/bootstrap.min.css" rel="stylesheet">

  </head>
  <body>

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
    <script src="js/bootstrap.min.js"></script>
  </body>

  <div class="jumbotron text-center">
  <h1>Hospital Information System</h1>

</div>

<div class="container" align="center">

<div class="card" style="width:400px" >

  <img src="images/avatar.png" alt="Card image" class="img-thumbnail">
  <div class="card-body">
    <h4 class="card-title" align="center">Login</h4>
    <p class="card-text">

    <form action="authenticate.php" method="POST">
   Username<br />
   <input type="text" name="username" />
   <br />
   Password<br />
   <input type="password" name="password" />
   <br />
   <br />
   <input class="btn btn-primary" type="submit"></a>
  </form>
    </div>

</div>

</div>
</html>

Authenticate.php

<?php
// Initialize the session

session_start();

// Check if the user is already logged in, if yes then redirect him to welcome page
if(isset($_SESSION["loggedin"]) && $_SESSION["loggedin"] === true){
    header("location: index.php");
    exit;
}

// Include config file
include "connection.php";

// Define variables and initialize with empty values
$username = $password = "";
$username_err = $password_err = "";

// Processing form data when form is submitted
if($_SERVER["REQUEST_METHOD"] == "POST"){

    // Check if username is empty
    if(empty(trim($_POST["username"]))){
        $username_err = "Please enter username.";
    } else{
        $username = trim($_POST["username"]);
    }

    // Check if password is empty
    if(empty(trim($_POST["password"]))){
        $password_err = "Please enter your password.";
    } else{
        $password = trim($_POST["password"]);
    }

    // Validate credentials
    if(empty($username_err) && empty($password_err)){
        // Prepare a select statement
        $sql = "SELECT username, password FROM PATIENT WHERE username = ?";

        if($stmt = mysqli_prepare($conn, $sql)){
            // Bind variables to the prepared statement as parameters
            mysqli_stmt_bind_param($stmt, "s", $param_username);

            // Set parameters
            $param_username = $username;

            // Attempt to execute the prepared statement
            if(mysqli_stmt_execute($stmt)){
                // Store result
                mysqli_stmt_store_result($stmt);

                // Check if username exists, if yes then verify password
                if(mysqli_stmt_num_rows($stmt) == 1){                    
                    // Bind result variables
                    mysqli_stmt_bind_result($stmt, $username, $hashed_password);
                    if(mysqli_stmt_fetch($stmt)){
                        if(password_verify($password, $hashed_password)){
                            // Password is correct, so start a new session
                            session_start();

                            // Store data in session variables
                            $_SESSION["loggedin"] = true;
                            $_SESSION["username"] = $username;                            

                            // Redirect user to welcome page
                            header("location: index.php");
                        } else{
                            // Display an error message if password is not valid
                            $password_err = "The password you entered was not valid.";
                        }
                    }
                } else{
                    // Display an error message if username doesn't exist
                    $username_err = "No account found with that username.";
                }
            } else{
                echo "Oops! Something went wrong. Please try again later.";
            }
        }

        // Close statement
        mysqli_stmt_close($stmt);
    }

    // Close connection
    mysqli_close($conn);
}
?>

Connection.php

<?php


  // server details hidden

  // Create connection
  $conn = mysqli_connect($servername, $username, $password, $dbname);

  // Check connection
  if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
  }
?>

0 个答案:

没有答案