PHP多用户级登录页面

时间:2014-11-04 15:32:26

标签: php login

我是PHP新手,我一直坚持这一点,所以感谢任何帮助。

在我的数据库中,我有4个user_levels:

ID 名称 1 管理 2 会员 3 出纳员 4 unregistered_user

问题是,当任何用户登录时,它会将其重定向到页面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;

        }

    }

    ?>

1 个答案:

答案 0 :(得分:2)

从users表中获取用户记录,将权限设置为$ _SESSION变量,并根据他们的权限将其重定向到正确的页面。

当然你需要更改表名,表单字段名,列名,auth方法等......

修改

因为您添加了代码,所以这是一个更新版本,基于您的代码:

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

}