So I am making a simple login page in PHP. I have 2 tables, one for Staff (admin, manager, etc...) and one for Customer, both have 3 columns in common: username, password, role. When the user login, role will be set in session to redirect the user to the appropriate page. Currently my code is as below:
function.php
function queryMySQL($query)
{
global $conn;
$result = $conn->query($query);
if(!$result)
{
die($conn->error);
}
return $result;
}
function passwordToToken($password)
{
global $salt1;
global $salt2;
$token = hash("ripemd128", "$salt1$password$salt2");
return $token;
}
login.php
<?php
require_once 'function.php'
$user = $_POST['user'];
$pass = $_POST['pass'];
$token = passwordToToken($pass); //encrypt password
$query = "Select userId, username, password, role
from users
where username = '$user'
and password = '$token'
union
Select customerId, username, password, role
from customer
where username = '$user'
and password = '$token'";
$result = queryMySQL($query); //function return mysqli_query
if($result->num_rows == 0)
{
$error = "Username/password is invalid";
echo $error;
} else {
session_start();
$_SESSION['uId'] = mysqli_fetch_array($result)[0];
$_SESSION['user'] = $user;
$_SESSION['pass'] = $pass;
$role = mysqli_fetch_array($result)[3];
if($role != 'admin' || $role != 'staff' || $role != 'manager')
{
$_SESSION['role'] = 'customer';
} else {
$_SESSION['role'] = $role;
}
echo $role;
}
?>
My problem is that when the user login correctly, the $role
variable is blank, however if I echo the $_SESSION['uId']
it does return result (the userID), and even if I change the $_SESSION['uId']
value to mysqli_fetch_array($result)[3] I still get the correct result when echo (the user role).