会话if语句在一个页面上工作而在另一个页面上工作

Before we start, I'm a beginner. I'm sure it's a simple solution but just not for me.

I'm using a session so no one can't access an admin page unless they're an admin.

The code is:

if ( $_SESSION['admin'] != 1 ) {
$_SESSION['message'] = "You're not admin!";
header("location: ../error.php");    
}

Which works perfectly. regular users can't access it but admins can. BUT.

In my main cpanel I want to have a button linking to the admin panel that will only appear if the user is admin. I did the same piece of code:

if ( $_SESSION['admin'] != 1 ) {
echo '<a href="admin/index.php"><button class="butto button-bloc" name="admin"/>Admin Panel</button></a>';
}

Problem is, it seems like the button shows up whether the user is an admin or not and now I think that my code is just trying to make me blow my lid as I spent a full hour trying to find what the f-- is on the go.

Any help would be greatly appreciated! I'm sure someone who's good with PHP will have it solved in under a minute lol

The condition you have written is same for user who is admin and not an admin

Please check the condition

For having a button linking to the admin panel that will only appear if the user is admin.

if ( $_SESSION['admin'] == 1 ) { // you have written $_SESSION['admin'] != 1
echo '<a href="admin/index.php"><button class="butto button-bloc" name="admin"/>Admin Panel</button></a>';
}

You can use

if (isset($_SESSION['admin']) && ($_SESSION['admin'] == 1 )) {
    echo '<a href="admin/index.php"><button class="butto button-bloc" name="admin"/>Admin Panel</button></a>';
}

This way the button will show when you set the $_SESSION['admin'] and if it is TRUE (i.e. 1).

Try This:

if (isset($_SESSION['admin']) {
    if ( ($_SESSION['admin'] == 1) ) {
        echo '<a href="admin/index.php"><button class="butto button-bloc" name="admin"/>Admin Panel</button></a>';
    }
}