我的数据库中有一个名为users的表,表中有一个User_Type来区分用户和admin。我的主要问题是,当User_Type ='admin'在登录时检测到管理员时,我可以让管理员重定向到/admin/home.php目录吗?如果User_Type中的用户是管理员,则转到index.php? >
这是我的loginprocess.php代码:
<?php
ob_start();
session_start();
?>
<html>
<body>
<?php
//DATABASE CONNECTION
$mysqli = new mysqli("localhost", "root", "", "starbucks");
if ($mysqli->connect_errno)
{
echo "Failed to connect to MySQL: (" .
$mysqli->connect_errno . ") " . $mysqli->connect_error;
}
//INPUT VARIABLES
$Username = "";
$Password="";
$User_Type = $row['User_Type'];
//READ INPUTS
if (isset($_POST['Username']))
{
$Username = $_POST['Username'];
}
else
{
// error in Username, go to login page
$_SESSION["errormsg"]='Wrong username or password, please try again';
header( 'Location: login.php' );
}
if (isset($_POST['Password']))
{
$Password = $_POST['Password'];
}
else
{
// error in Password, go to login page
$_SESSION["errormsg"]='Wrong username or password, please try again';
header( 'Location: login.php' );
}
//CHECK INPUTS
if (empty($Username))
{
// error in Username, go to login page
header( 'Location: login.php' );
}
if (empty($Password))
{
// error in Password, go to login page
header( 'Location: login.php' );
}
// VERIFY USER IN DATABASE
$query = "SELECT * FROM users WHERE " .
"Username='" . $Username . "' ".
"AND Password='" . $Password . "';";
//echo "query:" . $query . "<BR>";
$res = $mysqli->query($query);
if ($res)
{
if ($res->num_rows>0)
{
// a user is found, create user session
$res->data_seek(0);
$row = $res->fetch_assoc();
$_SESSION['users']=array(
"Username" => $row["Username"],
"Email" => $row["Email"]
);
// a user is found, go to catalog
header( 'Location: catalog3.php' );
}
else
{
// no user is found, go to login page
header( 'Location: login.php' );
}
}
else
{
// no user found, go to login page
header( 'Location: login.php' );
}
?>
</body>
</html>