2016-11-22 58 views
-1

我怎麼能實現rank函數,例如,如果用戶有秩「1」,他具有管理員權限,如果他有「0」,他是普通用戶使用排名

我知道我必須使用類似

$sql = " 
SELECT * 
    from users 
WHERE username LIKE '{$username}' 
    AND password LIKE '{$password}' 
LIMIT 1"; 

但不知道如何使用它正確

我的數據庫看起來像

username password rank 
tom  tom1  0 
john  john1 1 

<html> 
<head> 
    <title>SRC Centr</title> 
</head> 
<body> 
<h1><center>SRC Centr</h1> 
<?php 
if (!isset($_POST['submit'])){ 
?> 
<!-- The HTML login form --> 
    <form action="<?=$_SERVER['PHP_SELF']?>" method="post"> 
     <center>Username: <input type="text" name="username" required /><br /><center> 
     <center>Password: <input type="password" name="password" required /><br /><center> 

     <input type="submit" name="submit" value="Login" /> 
    </form> 
<?php 
} else { 
    require_once("db_const.php"); 
    $mysqli = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME); 
    # check connection 
    if ($mysqli->connect_errno) { 
     echo "<p>MySQL error no {$mysqli->connect_errno} : {$mysqli->connect_error}</p>"; 
     exit(); 
    } 

    $username = $_POST['username']; 
    $password = $_POST['password']; 
    $rank = $_POST['rank']; 

    $sql = "SELECT * from users WHERE username LIKE '{$username}' AND password LIKE '{$password}' LIMIT 1"; 
    $result = $mysqli->query($sql); 
    if (!$result->num_rows == 1) { 
     echo "<center><h3>Incorrect login</h3></center>"; 
    } else { 
     if ($rank == "0"){ 
    header("location: app.php"); 
} 
else if ($rank == "1"){ 
header("location: index.php"); 
} 





} 
} 
?>  
</body> 
</html> 

謝謝:)

+1

修復你的MySQL注入的問題,或任何人誰​​願意成爲一個管理員會。此外,不要存儲明文密碼。 – CollinD

+1

你沒有從任何地方獲得'$ _POST ['rank']'。從數據庫中選擇它,而不是通過POST獲取它。你真的應該哈希你的密碼,而不要提這裏的SQL漏洞。 – Qirel

+0

你能舉幾個例子嗎我該怎麼做? – pregot

回答

0

僞代碼:

if((SELECT rank FROM users WHERE username = {username} and password = {password}) == 1) { 
    set rank of user to admin 
} 

這是你問什麼?

0

我並不確切地知道你想要什麼,但你可以試試這個

$sql ="SELECT username, rank from users 
     WHERE 
      username ='".$_post["username"]."' AND 
      password ='".$_post["password"]."'"; 
$result = $mysqli->query($sql); 
if($result == 1) { 
    if($user = mysqli_fetch_array($result)) 
     if($user["rank"] == 1){ 
      #Do something 
     } else { 
      #DO somthing 
     } 
    } 
} else { 
    die ("Error"); 
} 

讓我知道你是否需要另一件事