Swapping links based on usertype PHP/MYSQLI

Hello I am trying to make it so if a ‘admin’ logs on it shows a button for the admin home page ‘ahome.php’ and if a user logs in it only shows the home button for ‘home.php’. I am currently not getting errors but it isn’t showing any buttons.

My header.php is:

<?php 
                if ($usertype == 'admin'){
                        echo '<a href="ahome.php" class=" w3-bar-item w3-button w3-round w3-mobile w3-russo"><h4><i class="fa fa-home"></i> HOME</h4></a>';
                    } else {
                        echo '<a href="home.php" class=" w3-bar-item w3-button w3-round w3-mobile w3-russo"><h4><i class="fa fa-home"></i> HOME</h4></a>';
                    }
            ?>

And the code to pull from the login form is:

$usertype = $_SESSION[‘user_type’];

This is how I think I am pulling the ‘user_type’ from my login in function :

$query = “SELECT * FROM users WHERE username = ‘$username’ AND password = ‘$password’ LIMIT 1”;
$results = mysqli_query($conn, $query);

  if (mysqli_num_rows($results) == 1) {
  	// Admin Check
  	$logged_in_user = mysqli_fetch_assoc($results);
                  
  	if ($logged_in_user['user_type'] == 'admin') {
  		$_SESSION['user'] = $logged_in_user;
                         $_SESSION['username'] = $username;
                         $_SESSION['user_type'] = $usertype;
  		$_SESSION['success']  = "You are now logged in";
  		header('location: ahome.php');		  
  	}else{
  		$_SESSION['user'] = $logged_in_user;
                         $_SESSION['username'] = $username;
                         $_SESSION['user_type'] = $usertype;
  		$_SESSION['success']  = "You are now logged in";

  		header('location: home.php');
  	}
  }

my ‘username’ is functioning and at one point I echoed the ‘$usertype’ in the header and it would give me the correct user type. I am just not sure what I am doing wrong i have spent 2 or 3 hours trying to figure this out. Any help would be amazing.

Not sure about your real problems…

but Id’ start out by cleaning up that header file… its terrible. (sorry)… Why would you use some many PHP tags off & on over and over again?

Not to mention the non-valid classes?

<?
if ($usertype == 'admin'){
	echo '<a href="ahome.php" class="w3-bar-item w3-button w3-round w3-mobile w3-russo"><h4><i class="fa fa-home"></i> HOME</h4></a>';
}
if ($usertype == 'user'){
	echo '<a href="home.php" class="w3-bar-item w3-button w3-round w3-mobile w3-russo"><h4><i class="fa fa-home"></i> HOME</h4></a>';
}
?>

Keep the value echo’d out in the header, so you can watch if it changes from page to page or anything else suspect.

I cleaned it up and put if usertype = admin show admin home link, then else just show the user home link instead of two if statements. It just shows the user home link. It is like is doesn’t see the logged in user is a admin. I will keep trying to figure it out and read more in the php manual.

I just can’t get my head around it, if I log in as a admin to the site it takes me to the admin page so it knows I am an admin. Is there a way to change the links before the header loads after submit in the login script? I will update the above scripts to show what I changed. Sorry if this is confusing I am trying to describe what I am trying to do the best I can

Without being able to test, its hard to say.

Try not using the variable at all… and accessing the $_SESSION variable directly…

if thats not working, dump the $_SESSION data and see if the value is really being set as you expect it to be.

1 Like

Okay it is working, not really sure bow but I will explain.

I echoed var_dump($_SESSION) and found out my user_type was null but also noticed that there was a $_SESSION[‘user’] (I didn’t call it it must have gotten carried over from login). I am still new to Sessions. Anyways it had my user_type in it so I did:

<?php 
                if ($_SESSION['user']['user_type'] == 'admin'){
                        echo '<a href="ahome.php" class=" w3-bar-item w3-button w3-round w3-mobile w3-russo"><h4><i class="fa fa-home"></i> HOME</h4></a>';
                    } else {
                        echo '<a href="home.php" class=" w3-bar-item w3-button w3-round w3-mobile w3-russo"><h4><i class="fa fa-home"></i> HOME</h4></a>';
                    }
            ?>

This worked after spending all day on this problem I have come away knowing the var_dump which is amazing! PHP is F****** awesome I love this stuff. I would like to learn how my ‘user’ session got carried over and will look into that and read even more about it then I did before because I clearly missed some stuff.

Thank you very much Whisper, you don’t know how much I appreciate your help!

Sponsor our Newsletter | Privacy Policy | Terms of Service