gpt4 book ai didi

php - mysql根据php角色将用户重定向到不同页面

转载 作者:行者123 更新时间:2023-11-29 11:13:34 24 4
gpt4 key购买 nike

我需要根据数据库中赋予用户的角色将用户重定向到不同的页面。登录页面仅提交用户名和密码。我必须从数据库中获取角色,如下所示:

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

这是代码:

<?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>

最佳答案

您应该检查用户角色。这是一个如何检查它的示例。

P.S adminfile.php 和 anotherfile.php 是您应该重定向用户的位置,并且可以是您想要的任何内容。

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!";
}

关于php - mysql根据php角色将用户重定向到不同页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40128261/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com