我是PHP的新手,一直被这个问题困扰着,所以我们非常感谢您的帮助。
在我的数据库中,我有4个用户级别:
id名称1.管理2.成员3.出纳员4.unregistered_user
问题是,当ANY用户登录时,它会将其重定向到profile.php页面。我需要每个用户都有其他权限,搜索结果等。我该怎么做?
login.php
<!doctype html>
<html>
<head>
<title>Login</title>
</head>
<body>
<?php include 'connect.php'; ?>
<?php include 'functions.php'; ?>
<?php include 'title_bar.php'; ?>
<h2>Login:</h2>
<form method='POST'>
<?php
if(isset($_POST['submit'])){
$username=$_POST['username'];
$password=$_POST['password'];
if(empty($username) or empty($password)){
echo "You missed something!";
}else{
$check_login=mysql_query("SELECT id, type FROM users WHERE username='$username' AND password='$password'");
if(mysql_num_rows($check_login)!=0){
$run=mysql_fetch_array($check_login);
$user_id=$run['id'];
$type=$run['type'];
if($type=='d'){
echo "<p>Account not activated!</p>";
}else{
$_SESSION['user_id']=$user_id;
header("location: profile.php");
}
}else{
echo "<p>Wrong information!</p>";
}
}
}
?>
<br/>
username:
<br/>
<input type='text' name='username'><br/>
<br/>
pass:
<br/>
<input type='password' name='password'><br />
<br><input type='submit' name='submit' value='Login'><br/>
</form>
</body>
</html>
session_start.php
<?php
session_start();
function loggedin(){
if(isset($_SESSION['user_id'])&& !empty($_SESSION['user_id'])){
return true;
}else{
return false;
}
}
?>
从users表中获取用户的记录,将permisson设置为$_SESSION变量,并根据他们的权限将他们重定向到正确的页面。
当然,您需要更改表名、表单字段名、列名、身份验证方法等
编辑:
因为你添加了你的代码,所以这是一个基于你的代码的更新版本:
if ($type == 'd') {
echo "<p>Account not activated!</p>";
} else {
$_SESSION['user_id'] = $user_id;
$_SESSION['type'] = $run['type'];
switch ($run["type"]) {
case 1:
//admin
header("Location: admin.php");
die();
break;
case 2:
//member
header("Location: member.php");
die();
break;
case 3:
//cashier
header("Location: cashier.php");
die();
break;
case 4:
header("Location: unregistered.php");
die();
//unregistered user
break;
}
}