簡體   English   中英

在提交按鈕單擊登錄詳細信息后如何將用戶重定向到主頁

[英]how to redirect user to the home page after login details on submit button click

如果數據庫中存在用戶名和密碼,我試圖在登錄后將用戶引導到管理儀表板。 如果沒有,則將用戶引導回登錄頁面。 但是當用戶輸入他的詳細信息而不是進入管理儀表板頁面時不起作用,即使用戶詳細信息在數據庫中,它也會被引導回登錄頁面。 問題出在 admin-dashboard.php 文件中,如果我注釋掉“header('location:index.php');” 它運行良好,但用戶無需登錄 url 搜索欄即可訪問管理儀表板,我不希望這樣

這是我的索引。php

    <?php
session_start();
if(isset($_SESSION['username'])){
   header('location:admin-dashboard.php');
    exit();
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Login | Admin</title>
    <!-- <link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/v/dt/dt-1.10.22/datatables.min.css"/> -->
    <link rel="stylesheet" href="assets/css/style.css" type="css/text">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.4.1/css/bootstrap.min.css"/>
<style type="text/css">
html,body{
    height:100%;

}
</style>
</head>
<body class="bg-dark">
    <div class="container h-100">
        <div class="row h-100 align-items-center justify-content-center">
            <div class="col-lg-5">
                <div class="card border-danger shadow-lg">
                    <div class="card-header bg-danger">
                        <h3 class="m-0 text-white"><i class="fas fa-user-cog"></i>&nbsp;Admin Panel Login</h3>
                    </div>
                    <div class="card-body">
                        <form action="action" method="post" class="px-3 " id="admin-login-form"> 
                            <div id="adminLoginAlert"></div>
                            <div class="form-group">
                                <input type="text" name="username" class="form-control 
                                form-control-lg rounded-2" placeholder="Username" required autofocus>
                            </div>
                            <div class="form-group">
                                <input type="password" name="password" class="form-control 
                                form-control-lg rounded-2" placeholder="Password" autocomplete= required>
                            </div>
                            <div class="form-group">
                                <input type="submit" name="admin-login" class="btn btn-danger
                                btn-block btn-lg rounded-2" value="Login" id="adminLoginBtn">
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.4.1/js/bootstrap.bundle.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.12.1/js/all.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@8"></script>
<script type="text/javascript">
    $(document).ready(function(){
// sending ajax request to server
    $("#adminLoginBtn").click(function(e){
    if($("#admin-login-form")[0].checkValidity()){
            e.preventDefault();
                $(this).val('Please Wait...');
                $.ajax({
                    url:'assets/php/admin-action.php',
                    method:'post',
                    data:$("#admin-login-form").serialize()+'&action=adminLogin',
                    success:function(response){
                        if($.trim(response) == 'register'){
                            window.location = 'admin-dashboard.php';
                        }
                        
                        if(response === 'admin_login'){
                            window.location = 'admin-dashboard.php';
                        }
                        else{
                            $("#adminLoginAlert").html(response);
                        }
                        $("#adminLoginBtn").val('Login');
                    }
                });
            }
        });
    });
</script>
</body>
</html>

我的管理儀表板.php

 <?php
session_start();
if(!isset($_SESSION['username'])){
   header('location:index.php'); 
   exit();
  
}
?>
<a href="assets/php/logout.php">Logout</a>

我的 config.php

  <?php
class Database {
    
    private $dsn = "mysql:host=localhost;dbname=database_user_system";
    private $dbuser = "root";
    private $dbpass = "";

    public $conn;

    public function __construct(){
        try{
            $this->conn = new PDO($this->dsn,$this->dbuser,$this->dbpass);
               
              

        }catch (PDOExeception $e) {
            echo 'Error :'.$e->getMessage();

        }
        return $this->conn;
    }
    // Checking Input 
    public function test_input($data){
        $data = trim($data);
        $data = stripslashes($data);
        $data = htmlspecialchars($data);
        return $data;
    }

    // Error success message alert
    public function showMessage($type,$message){
        return '<div class="alert alert-'.$type.' alert-dismissible "> 
                    <button type="button" class="close" 
                    data-dismiss="alert">&times;</button>
                    <strong class="text-center"> '.$message.' </strong>
                    
                     </div>';
    }

    
}




?>

我的注銷。php

<?php
session_start();
unset($_SESSION['username']);
header('location:../../index.php'); 


?>

我的管理員操作.php

    <?php
require_once 'admin-db.php';

$admin = new Admin();

    // Handle admin login ajax Request

if(isset($_POST['action']) && $_POST['action'] == 'adminLogin'){
    $username = $admin->test_input($_POST['username']);
    $password =$admin->test_input($_POST['password']);


    $hpassword = sha1($password);
    $loggedInAdmin = $admin->admin_login($username,$hpassword);

    if($loggedInAdmin !=null){
        echo 'admin_login';
        $_SESSION['username']= $username;
    }
    else {
      echo  $admin->showMessage('danger', 'Username or Password is Incorrect!');}
    }

    ?>

我的 admin-db.php

<?php
require_once 'config.php';

//creating new object of admin class in admin-action.php
class Admin extends Database {
    // Admin login

    public function admin_login($username, $password)
    {
       $sql = "SELECT username,password FROM admin WHERE username = :username AND 
       password = :password";

       $stmt = $this->conn->prepare($sql);
       $stmt->execute(['username'=>$username,'password'=>$password]);
       $row = $stmt->fetch(PDO::FETCH_ASSOC);

       return $row; 
    }
}
?>

我認為您需要您的位置 header 成為 PHP 文檔中指定的 HTTP 1.1 的絕對路徑。 header('Location: http://localhost/admin-dashboard.php'); 或在出口內調用, exit(header('Location: http://localhost/admin-dashboard.php'));

請參閱此處了解更多詳細信息

首先,如果結果大於 0,則從數據庫中獲取數據,它會使用以下代碼重定向

<?php
require_once 'config.php';

//creating new object of admin class in admin-action.php
class Admin extends Database {
    // Admin login

    public function admin_login($username, $password)
    {
       $sql = "SELECT username,password FROM admin WHERE username = :username AND 
       password = :password";

       $stmt = $this->conn->prepare($sql);
       $stmt->execute(['username'=>$username,'password'=>$password]);
       $row = $stmt->fetch(PDO::FETCH_ASSOC);
if($row > 0){
header('location:index.php);
}else{
header('location:error.php);
}

  }
}
?>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM