Skip to content
Advertisement

PHP I want to restrict a button in the navbar to Admin users only

Currently making a blog websit and wanted to add an administrator locked button in the navbar that allows them to delete users there is onlly 1 Admin whos username will be “Admin”.

Right now I have something like this:

<nav class="navbar">
    <p class="logo">BLOG</p>
    <ul class="navbar-list">
        <?php
            if(isset($_SESSION['username'] == "Admin"))
            {
                echo ($_SESSION["username"]),
                
                '
                    <li class="navbar-item"><a href="index.php">Home</a></li>
                    <li class="navbar-item"><a href="adminpage.php">master</a></li>
                    <li class="navbar-item">
                        <form action="includes/logout.php" method="POST">
                            <button class="btn btn-logout" type="submit" name="submit">Logout</button>
                        </form>
                    </li>
                ';
            }

            else if (isset($_SESSION['username'])) {
                echo ($_SESSION["username"]),
                
                '
                    <li class="navbar-item"><a href="index.php">Home</a></li>
                    <li class="navbar-item">
                        <form action="includes/logout.php" method="POST">
                            <button class="btn btn-logout" type="submit" name="submit">Logout</button>
                        </form>
                    </li>
                ';
            } else {
                echo '
                    
                    <li class="navbar-item"><a href="index.php">Home</a></li>
                    <li class="navbar-item"><a href="signup.php">Sign Up</a></li>
                    <li class="navbar-item"><a href="login.php">Sign In</a></li>
                ';
            }
        ?>
    </ul>
</nav>

I keep getting this error Fatal error: Cannot use isset() on the result of an expression (you can use “null !== expression” instead) in C:xampphtdocspdo-blog-masterincludesnavbar.php on line 12

Any help will be appreciated.

Advertisement

Answer

You have an Error here:

if (isset($_SESSION['username'] == "Admin"))

You are trying to check if the result of the expression is set (true or false) which doesn’t work because isset() can’t used that way.

https://www.php.net/manual/en/function.isset.php

Change this to:

if (isset($_SESSION['username']) && $_SESSION['username'] === "Admin")

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement