dongyuling0312 2016-10-19 10:07
浏览 35
已采纳

mysql将用户重定向到基于角色php的不同页面

I need to redirect users to different pages based on the roles given to them in the database. Only the username and password is submitted on the login page. I have to fetch the role from the database which looks like this:

username  |  password  |  role
admin1       admin1       admin
alex12       alex12       (nothing to normal users)

Here is the code:

<?php 
    session_start();
    // conectare la baza de date
    $db = mysqli_connect("localhost", "root", "", "inregistrare");
    if (isset($_POST['login_btn'])) {
        $username = mysqli_real_escape_string($db,$_POST['username']);
        $password = mysqli_real_escape_string($db,$_POST['password']);
        $password = md5($password); // parola cryptata
        $sql = "SELECT * FROM users WHERE username='$username' AND password='$password'";
        $result = mysqli_query($db, $sql);
        if (mysqli_num_rows($result) == 1) {
            $_SESSION['message'] = "Te poti Conecta!";
            $_SESSION['username'] = $username;
            header("location: clasa.php"); //spre o pagina
            
        }else{
            $_SESSION['message'] = "Parola gresita!";
        }
    }
?>


<head>
    <title>Conectare</title>
    <link rel="stylesheet" type="text/css" href="./css/index-style.css">
</head>
<body>
<?php
    if (isset($_SESSION['message'])) {
        echo "<div id='error_msg'>".$_SESSION['message']."</div>";
        unset($_SESSION['message']);
    }
?>


<form method="post" action="clasa.php"> <!-- modifica si aici cand modifici mai sus la php-->
    <table align="center">
        
        <tr>
            <th id="titlu" class="titlu" colspan="2">Conectare</th>
        </tr>
        <tr>
            <td class="border">Username:</td>
            <td class="border"><input type="text" name="username" class="text-input" size="20"></td>
        </tr>

        <tr>
            <td class="border">Password:</td>
            <td class="border"><input type="password" name="password" class="text-input" size="20"></td>
        </tr>
        <tr>
            <td class="spatiu"></td>
            <td class="spatiu"></td>
        </tr>
        <tr>
            <td><button class="register" type="submit" formaction="./register.php">Inregistrare</button></td>
            <td><button class="connect" type="submit" name="login_btn">Conectare</button></td>
        </tr>
    </table>
</form>
</body>
</html>

</div>
  • 写回答

3条回答 默认 最新

  • dsxcv5652 2016-10-19 10:15
    关注

    You should check the user role. Here is an example how you can check it.

    P.S the adminfile.php and anotherfile.php is where you should redirect the user and can be whatever you want.

    if (mysqli_num_rows($result) == 1) {
            $_SESSION['message'] = "Te poti Conecta!";
            $_SESSION['username'] = $username;
            $user = mysql_fetch_assoc($result);
    
    
            if($user['role'] == 'admin'){
                header("location: adminfile.php");
            }else{
                header("location: anotherfile.php");
            }
    
    
        }else{
            $_SESSION['message'] = "Parola gresita!";
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器