Remco de Baas
Remco de Baas

Reputation: 61

How to let the admin see specific buttons

I am working on a dashboard button on my website. This button only shows up when anyone is logged in, but the meaning of the button is to only show it to the admin.

Here is a part of the login function:

 $query = $db->prepare("SELECT * FROM users WHERE userName = :userName AND userPassword =:userPassword");
        $query->bindParam(":userName", $username, PDO::PARAM_STR);
        $query->bindParam(":userPassword", $password, PDO::PARAM_STR);
        $query->execute();


        if ($query->rowCount() > 0) {
            $user = $query->fetch();
            $_SESSION['login'] = $user;
            header("location: ../../index.php");
            die();
        }
    }

And here is the part of the index that needs to show the dashboard button for the admin:

<?php
            if(!isset($_SESSION['login']) || $_SESSION['login']['userId'] ==  0)
            {
            echo '<a class="btn" name="login" href="functions/login/functions.php">Login</a>';


            }
            else
            {
                echo "<h3>Welkom " . $_SESSION['login']['userName']."</h3>";
                echo '<a class="btn" name="logout" href="functions/logout/logout.php">Logout</a>';
                echo '<a class="btn" name="dashboard" href="#">Dashboard</a>';
            }
            ?>

I cannot figure out what to do here. So if anyone can help I appreciate your help.

Upvotes: 1

Views: 48

Answers (1)

Alexis
Alexis

Reputation: 5831

You can do something like this

if(!isset($_SESSION['login']) || $_SESSION['login']['userId'] ==  0)
{
   if($_SESSION['login']['userAdmin']==1)
    { 
    //is admin (content for admin)
   }
   else
   {
    //not admin (content for no admin)
   }
   //content for both 
}

Upvotes: 1

Related Questions