首页 > 解决方案 > PHP登录系统验证问题

问题描述

我正在研究简单的登录系统。我的代码以前运行良好,但现在我添加了在用户登录时显示 php 页面的功能。我正在使用 MySQL 来存储登录信用。代码分为三个部分。登录表单,连接到 MySQL 和身份验证。我还添加test.html了测试系统(错误的密码 -> 重定向到test.html)。

我的代码 auth.php

<?php      
        include('connect.php');  
        $username = $_POST['user'];  
        $password = $_POST['pass'];  
          
            //to prevent from mysqli injection  
            $username = stripcslashes($username);  
            $password = stripcslashes($password);  
            $username = mysqli_real_escape_string($con, $username);  
            $password = mysqli_real_escape_string($con, $password);  
          
            $sql = "select *from userDetails where username = '$username' and password = '$password'";  
            $result = mysqli_query($con, $sql);  
            $row = mysqli_fetch_array($result, MYSQLI_ASSOC);  
            $count = mysqli_num_rows($result);  

            function function_alert($message) {
                echo "<script>alert('$message');</script>";
            }
              
            if($count == 1){  
                session_regenerate_id();
                $_SESSION['loggedin'] = TRUE;
                header('Location: main.php');
            }  
            else{  
                function_alert("Nesprávné heslo"); 
                header("Location: https://blazeplay.eu/ktest/panel/test.html");
                echo "Nesprávné přihlašovací údaje";
die();
            }     
    ?>  

main.php(这是登录用户的主页)

<?php

    include('auth.php'); 
    $disUser = $username;

    session_start();

    if (!isset($_SESSION['loggedin'])) {
        header('Location: test.html');
        exit;
}

?>

还有一些 HTML...

非常感谢你帮助我。

标签: phpmysqlauthentication

解决方案


推荐阅读