I am constructing a social networking site. Users who register and log in correctly are redirected to home.php
with a $_SESSION
made accordingly.
But I have manually made an admin user with the username of freddy
(the username is required to log in). What I am trying to state is that "if the username is equal to freddy, then take him to admin_home.php
".
What I have tried is to create two separate $_SESSION
's.
$_SESSION
created for normal user:
// if the user credentials are correct, log the user in:
$_SESSION["user_login"] = $user_login;
header( "Location: home.php" ); // refresh page
exit;
}
$_SESSION
created for admin:
if ($account_type == "admin"){
// Create seperate session for admin
$_SESSION["user_login"] = $admin_login;
header( "Location: admin_home.php" ); // refresh page
exit;
}
Full query:
<?php
$user_query = mysqli_query ($connect, "SELECT * FROM users WHERE username = '$user_login' AND password = '$decrypted_password' AND closed='no' LIMIT 1");
$check_for_user = mysqli_num_rows ($user_query); // checking to see if there is infact a user which those credentials in the DB
if ($check_for_user==1){
while ($row = mysqli_fetch_array($user_query)){
$user_id = $row['id'];
$account_type = $row['account_type'];
}
// if the user credentials are correct, log the user in:
$_SESSION["user_login"] = $user_login;
header( "Location: home.php" ); // refresh page
exit;
}
if ($account_type == "admin"){
// Create seperate session for admin
$_SESSION["user_login"] = $admin_login;
header( "Location: admin_home.php" ); // refresh page
exit;
}
else {
// if user row does not equal 1 ...
echo "<div class='wrong_login'>
<p> Username or password is incorrect, please try again. </p>
</div>";
exit();
}
}
?>
With the current code, logging in with the username freddy
- which should take me to admin_home.php
, takes me to home.php
which is not what I want.