用户和管理员的不同登录表单

时间:2017-03-30 02:28:37

标签: php html

如何区分用户和管理员的重定向页面?

数据库表:

enter image description here

这是我的代码;

<?php

   require ("config.php");

   //connect to mysql
   $link = mysqli_connect($h,$u,$p,$db)
   OR die(mysql_error());

  $query = "select * from login where username='".$_POST["uname"]."'and password='".$_POST["pswd"]."'";

  $result = mysqli_query($link,$query); 
  $count =mysqli_num_rows($result);
  //while($row = mysqli_fetch_assoc($result)) {
//}
   if($count==1){
    session_start();
    $_SESSION['username'] = $_POST["uname"];
    $_SESSION['password'] = $_POST["pswd"];
    header("Location: index.html");

   }
    else{
    echo "<script language=\"JavaScript\">\n";
    echo "alert('Username or Password was incorrect!');\n";
    echo "window.location='login.html'";
    echo "</script>";
   }
?>

我如何分离用户和管理员登录,我宁愿在这里新,所以你的解释/代码非常感谢!

提前致谢!

2 个答案:

答案 0 :(得分:1)

您可以在数据库中添加一个字段,该字段是用户的重定向页面;当用户登录时,它只是重定向到该页面。或者你可以这样做。

<?php session_start();

   require ("config.php");

   //connect to mysql
   $link = mysqli_connect($h,$u,$p,$db)
   OR die(mysql_error());

  $query = "select * from login where username='".$_POST["uname"]."'and password='".$_POST["pswd"]."'";

  $result = mysqli_query($link,$query); 
  $count =mysqli_num_rows($result);
  while($row = mysqli_fetch_assoc($result)) {
    $userType = $row['usertype'];
  }
  if($count==1){
    $_SESSION['username'] = $_POST["uname"];
    $_SESSION['password'] = $_POST["pswd"];
     if ($userType=='admin') {
       header("Location: index.html"); //whereever you want to take admins
      } else {
     header("Location: index.html"); //wherever you want to take all users
     }


   }
    else{
    echo "<script language=\"JavaScript\">\n";
    echo "alert('Username or Password was incorrect!');\n";
    echo "window.location='login.html'";
    echo "</script>";
   }
?>

在session_start();永远应该是文件中的第一件事,仅供参考。

答案 1 :(得分:0)

虽然我不是mysqli的用户,但这可能会指导您。只需将其转换为mysqli版本

即可
if('admin' == $row['usertype']) {
    header('Location:admin.php');
    exit;
}
if('user' == $row['usertype']) {
    header('Location:user.php');
    exit;
}
相关问题