2014-11-04 144 views
0

我是PHP新手,我一直堅持這一點,所以任何幫助表示讚賞。PHP多用戶級別登錄頁面

在我的數據庫我有4個user_levels:

ID 名管理成員出納 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

您需要張貼一些代碼才能獲得幫助。 – Mark 2014-11-04 15:34:04

+3

請向我們顯示您的代碼。但正如我想的那樣,從用戶表中獲取用戶記錄,將權限設置爲$ _SESSION變量,並根據權限將其重定向到適當的頁面。 – vaso123 2014-11-04 15:34:45

+0

你只是顯示/隱藏用戶不允許看到的東西.. – Naruto 2014-11-04 15:35:28

回答

2

讓您的用戶從用戶表的記錄,設置在未經許可進入$ _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; 
    } 

} 
+0

謝謝你的快速回答! – mar1 2014-11-04 15:59:24

+0

我粘貼了這個,沒有任何反應,沒有重定向,它只停留在登錄表 – mar1 2014-11-04 15:59:50

+0

轉儲'run [「type」];'是它1或2或3或4? – vaso123 2014-11-04 16:01:03