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条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度