2014-12-06 164 views
0

我试图设置不同的访问使用用户和管理员,但我无法弄清楚如何让他们看起来不同,我试图设置tinyint数据库,其中1是管理员和0是用户,但我不认为我做对了。 (你在其他地方找到它的代码),所以如果有更好的方法,帮助将非常感激。先谢谢你。 php创建单独的用户/管理员登录

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>Homeowners Association</title> 

</head> 


<?php 
    if ($_SESSION['validUser'] == "yes")     
    //is this already a valid user? 
    { 

//turn off PHP and turn on HTML 
?>       
     <h1>Display Events Admin Options</h1> 
     <p><a href="insertEvent.html">Input New Events</a></p> 
     <p><a href="selectEventsProtected.php">List of Events</a></p> 
     <p><a href="logout.php">Logout</a></p> 

<?php //turn off HTML and PHP 
    } 
    else 
    { 
     if (isset($_POST['submitLogin']))   
     //Was this page called from a submitted form? 
     { 
      $inUsername = $_POST['loginUsername']; 
      //pull the username from the form 
      $inPassword = $_POST['loginPassword']; 
      //pull the password from the form 

      include ('dbConnect.php');    
      //Connect to the database 

      $sql = "SELECT * FROM homeowners WHERE username = '" . $inUsername . "' AND password = '" . $inPassword . "'";    
      //this SQL command will only work if BOTH the username and password on the table 

      $result = mysqli_query($link,$sql) or die("SQL Error " . mysqli_error($link) . "<p>SQL String: $sql</p>"); 

      if (mysqli_num_rows($result) == 1)  
      //If this is a valid user there should be ONE row only 
      { 
       $_SESSION['validUser'] = "yes";   
       //this is a valid user so set your SESSION variable 

//turn off PHP and begin HTML 
?> 
       <h1>Display Events Admin Options</h1> 
       <p><a href="insertEvent.html">Input New Event</a></p> 
       <p><a href="selectEventsProtected.php">List of Events</a></p> 
       <p><a href="logout.php">Logout</a></p> 

<?php //turn off HTML and turn on PHP        
      } 
      else          
      //This is an invalid user not in the database 
      { 
       echo "<h3>Invalid username or password. Please try again.</h3>"; //sets error message 
       //display login form again with the error message. 

//turn off PHP and begin HTML 
?> 

       <form method="post" name="login" action="login.php" > 
        <p>Username: <input name="loginUsername" type="text" /></p> 
        <p>Password: <input name="loginPassword" type="password" /></p> 
        <p><input name="submitLogin" value="Login" type="submit" /> <input name="" type="reset" />&nbsp;</p> 
       </form> 

<?php //turn off HTML and turn on PHP 
      }//end of checking for a valid user 
     }//end of checking for a submitted page 
     else //This page was not submitted so the user needs to se the sign on form to continue 
     { 
      //display the login form in the area below 

//turn off PHP and begin HTML   
?> 
      <h1>Login to access website</h1> 
      <form method="post" name="loginForm" action="login.php"> 
       <p>Username: <input name="loginUsername" type="text" /></p> 
       <p>Password: <input name="loginPassword" type="password" /></p> 
       <p><input name="submitLogin" value="Login" type="submit" /> <input name="" type="reset" />&nbsp;</p> 
      </form> 

<?php //turn off HTML and turn on PHP 
     }//ends if statement to check for form submit 
    }//end if checking for a valid user 

//turn off PHP and begin HTML 
?> 

</body> 
</html> 

我试着使用:

$query = mysqli_query("SELECT type FROM homeowners WHERE username = '$user'"); 
$gettype = mysqli_fetch_assoc($query); 
if($gettype["type"] == 0){ 
echo("user"); 
} 
elseif($gettype["type"] == 1){ 
echo("admin"); 
} 

,但我不知道有足够的了解TINYINT(我已经设置为输入)

回答

0

如果使用TINYINT没有问题,但我更喜欢枚举确定用户访问,因为它直接显示用户访问。不只是数字,我必须再次记住1,2,3是什么意思,或其他什么。

要显示管理页面或用户页面,您只需要一个$_SESSION变量来存储用户访问权限。

include 'dbConnect.php'; 

$query = mysqli_query($con, "SELECT type FROM homeowners WHERE username = '$user'"); 
$gettype = mysqli_fetch_assoc($query); 
if($gettype["type"] == 0){ 
    $_SESSION["userAccess"] = "user" 
} 
elseif($gettype["type"] == 1){ 
    $_SESSION["userAccess"] = "admin" 
} 

,并添加到您若

if ($_SESSION["validUser"] == "yes") { 
    if($_SESSION["userAccess"] == "admin") { 
     //show admin stuff or admin page 
    } elseif ($_SESSION["userAccess"] == "user") { 
     //show user stuff or user page 
    } 
} 

不要忘记在login.php和其他文件中使用session_start()在你的代码的顶部,如果你想使用$_SESSION


UPDATE

看到我更新的答案。 你mysqli_query()犯的错误,该方法需要2个参数是这样的:

$query = mysqli_query($con, "YOUR_QUERY"); 

$con是YOUR_DB_CONFIG_FILE.php。在你的情况dbConnect.php

+0

是这个答案使用tinyint或枚举(我不能使用导致数据库说每次我尝试时都有错误)? – Felicyia 2014-12-06 06:04:14

+0

这个答案是使用tinyint,你可以发布你的数据库表吗? – 2014-12-06 06:49:13

+0

我不知道如何将它张贴到我的服务器作为jpg http://felicyiamcclelland.com/introphp/homeowners/images/server.jpg – Felicyia 2014-12-06 06:58:46