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我该怎么做?太棒了!谢谢