使用PHP和MySQL进行密码保护


Password Protection Using PHP and MySQL

我正在创建一个包含登录和注册功能的web应用程序。有两个主要用户,客户端和1管理员。到目前为止,我已经成功地为客户端创建了一个注册页面,该页面链接到mySQL数据库。

以及客户端和管理员的登录页面。登录后,客户端或管理员将被重定向到各自的仪表板。

我现在面临的问题是,如果任何访问该网站的人在培训师面板的url中键入内容,他们将被授予完全访问权限和管理权限。我希望出现一条消息,上面写着"请登录"

这是我目前在"login.php"文件中使用的代码片段:

   <?php
if (!isset($_POST['submit'])){
?>
<!-- The HTML login form -->
    <center><form action="<?=$_SERVER['PHP_SELF']?>" method="post">
        <div class="form-group">
                <input type="text" name="username" id="username" class="form-control input-lg" placeholder="Username" tabindex="3">
            </div>
        <div class="form-group">
                 <input type="password" name="password" id="password" class="form-control input-lg" placeholder="Password" tabindex="5">
            </div>
        <br /> <br /><input type="submit" name="submit" class="btn btn-success btn-block btn-lg" value="Login" /> </center>
    </form>
<?php
} else {
    require_once("db_const.php");
    $mysqli = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
    # check connection
    if ($mysqli->connect_errno) {
        echo "<p>MySQL error no {$mysqli->connect_errno} : {$mysqli->connect_error}</p>";
        exit();
    }
    $username = $_POST['username'];
    $password = $_POST['password'];
    $tusername = $_POST['username'];
    $tpassword = $_POST['password'];
    $sql = "SELECT * from client WHERE Client_username LIKE '{$username}' AND Client_password LIKE '{$password}' LIMIT 1";
    $result = $mysqli->query($sql);
    if (!$result->num_rows == 1) {
        echo "<p>Invalid username/password combination</p>";
    } else {
        header('location:client_dash.html?msg=success');
    }
    $sql = "SELECT * from trainer WHERE trainer_username LIKE '{$tusername}' AND trainer_password LIKE '{$tpassword}' LIMIT 1";
    $result = $mysqli->query($sql);
    if (!$result->num_rows == 1) {
        echo "<p>Invalid username/password combination</p>";
    } else {
        header('location:trainer_dash.php?msg=success');
    }
}
?>

您可以使用SESSION变量

您的代码将更改为.

<?php  
    ob_start();
    session_start();
    require_once("db_const.php");
    $mysqli = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
    if ($mysqli->connect_errno) {
    echo "<p>MySQL error no {$mysqli->connect_errno} : {$mysqli->connect_error}</p>";
        exit();
    }
    $username = $_POST['username'];
    $password = $_POST['password'];
    $tusername = $_POST['username'];
    $tpassword = $_POST['password'];
    $sql = "SELECT * from client WHERE Client_username LIKE {$username}' AND Client_password LIKE '{$password}' LIMIT 1";
    $result = $mysqli->query($sql);
    if (!$result->num_rows == 1) 
    {
        echo "<p>Invalid username/password combination</p>";
    }
     else 
    {
        $_SESSION['username']=$username;
        header('location:client_dash.html?msg=success');
    }
    $sql = "SELECT * from trainer WHERE trainer_username LIKE '{$tusername}' AND trainer_password LIKE '{$tpassword}' LIMIT 1";
    $result = $mysqli->query($sql);
    if (!$result->num_rows == 1) 
    {
        echo "<p>Invalid username/password combination</p>";
    }
     else
    {
        $_SESSION['tusername']=$tusername;
        header('location:trainer_dash.php?msg=success');
    }

}

?>

仪表板上,如果是client ,则可以执行此操作

<?php 
    ob_start();
    session_start();
    if(!isset($_SESSION['username']))
    {
        die('Please log in first');
    }
    unset($_SESSION['username']);
    */rest code*/
?>

类似地,trainer仪表板

<?php 
    ob_start();
    session_start();
    if(!isset($_SESSION['tusername']))
    {
        die('Please log in first');
    }
    unset($_SESSION['tusername']);
    */rest code*/
?> 

您正在取消设置会话变量,因为如果您不这样做,它将只在第一次工作,因为在那之后,您的会话将被永久设置因此,您必须unset他们

您可以使用会话

在重定向之前,设置一个会话变量(不要忘记先用session_start()启动会话)

    //to make sure the session_id() is different everytime the user logs in
session_regenerate_id(); 
    //store the session_id in a variable
$_SESSION['trainer']=session_id();

在你的trainer_dash.php上,从开始

session_start();
if(!isset($_SESSION['trainer'])||$_SESSION['trainer']!=session_id()){
      echo 'You shouldn't be here';
}

到目前为止,答案很好,因为它们阻止了一个特定的url(或文件夹)。

如果有人在其中一个面板中输入链接,例如图片,图片将由web服务器传递,因为没有PHP脚本会禁止这样做。

如果您需要一个健壮的解决方案,请尝试将登录/身份验证作为应用程序前面的一个组件进行分离。如果可能,请使用web服务器筛选器。

在您的情况下,一个简单的解决方案可以是使用PHP调用基本身份验证并对仪表板进行密码保护。这里对此进行了解释:

http://php.net/manual/de/features.http-auth.php

如果web服务器没有看到有效的身份验证,那么在解析任何PHP之前,它将阻止对每种文件的每种访问。

如果您需要更复杂的解决方案,就无法对Apache的细节进行研究(可能使用重写规则或错误页),也无法尝试使用与应用程序分离的成品库。以下是有关应用程序防火墙和原理的知识来源:

https://www.owasp.org/index.php/Main_Page