Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 用户和管理员的不同登录形式_Php_Html - Fatal编程技术网

Php 用户和管理员的不同登录形式

Php 用户和管理员的不同登录形式,php,html,Php,Html,如何区分用户和管理员的重定向页面 数据库表: 这是我的密码 <?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["

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

数据库表:

这是我的密码

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

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

if('admin' == $row['usertype']) {
    header('Location:admin.php');
    exit;
}
if('user' == $row['usertype']) {
    header('Location:user.php');
    exit;
}

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

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

您的脚本有被删除的风险。请查看使用时发生的情况。使用数据库中的
usertype
字段。这将改变你的要求。@Irvin谢谢你的回复,呃,你能告诉我把
usertype
放在哪里吗?千万不要在数据库中以纯文本形式存储密码。使用encryption@Christophvh我该怎么做?太棒了!谢谢