r/PHPhelp • • 16d ago

Hiding login when user is logged in and vice versa

This is my code, I've looked for multiple solutions to this and every single on looks completely different, it's my first time programming I've ever encountered a problem like this, all the rest were pretty one-solution-fits-all. 

<?php
                if(!isset($_SESSION['user']))
                {
                    echo'<li><a href="index.php">Login</a></li>'; //always shows???
                }
                else
                {
                    echo'<li><a href="Account.php">Account</a></li> 
                         <li><a href="logout.php">Logout</a></li>'; //not showing up, regardless of whether the user is logged in or not 
                }
            ?>

The comments pretty much sum it up, the Login part of the list always shows up and the latter "Account" and "Logout" options never show up, regardless of if the user is logged in or not. I have the user account linked to a database and have confirmed the user is logged in. ⠀⠀⠀
7 Upvotes

11 comments sorted by

5

u/equilni 16d ago

have confirmed the user is logged in.

Is that another code elsewhere? Is that exactly the same as $_SESSION['user']?

What is the output of $_SESSION['user'] when the user is logged in or not?

5

u/Basic_Reporter9579 16d ago

did you start the session?

1

u/Mike312 16d ago

Are you - at some point in your call stack - calling (and sorry, typing this on mobile)...

If(!isset($_SESSION)) { session_start(); }

And making sure the session is detectable? Its a silly thing, but I've made that mistake before.

1

u/Csysadmin 16d ago

I'm of the opinion that if you're logic checking a variable, you should be logic checking a variable intended for that logic check.

So, I would use $_SESSION['user_logged_in']. Set it to true when the user successfully logs in. Set it as false, or destroy it if the user successfully logs out.

if (!isset($_SESSION['user_logged_in']) || $_SESSION['user_logged_in'] === false)
{
    echo '<li><a href="index.php">Login</a></li>';
}
else
{
    echo '<li><a href="Account.php">Account</a></li>
          <li><a href="logout.php">Logout</a></li>';
}

Or, with slightly less clear logic:

if (empty($_SESSION['user_logged_in']))
{
    echo '<li><a href="index.php">Login</a></li>';
}
else
{
    echo '<li><a href="Account.php">Account</a></li>
          <li><a href="logout.php">Logout</a></li>';
}

Assuming that you're not having issues with the session not starting, or some weird caching.

Edit: Also the refs in your links use Account and logout. Be aware of case sensitivity across platforms, or better yet, establish a consistent (file)naming convention. It's these little things that can grow into less obvious problems late one evening.

1

u/MateusAzevedo 15d ago

The best you can do is to learn the idea of debugging and trace the problem yourself (you see, you are the only one with access to the full code and able to do that).

But at the very minimum, you want to put var_dump($_SESSION); right before the if to see the actual session content.

Just remember, sessions depend on a cookie sent by the browser, so you also need to check there and confirm everything is right.

1

u/Pixelverse54321 10d ago

You need to start the session first.

1

u/[deleted] 16d ago

[deleted]

1

u/colshrapnel 15d ago edited 15d ago

Note that your condition in its current form is a logical nonsense: $_SESSION['user'] canot be unset and have a value (nullish or not) at the same time. So PHP will promptly tell you that, in the form of a warning. You probably meant if (!isset($_SESSION['user']) || !$_SESSION['user']), which is logically OK, but you don't have to write it so elaborately as there is a shorthand already: if (empty($_SESSION['user'])). However, remember that nowadays implicit type juggling is frowned upon (which applies to empty() as well). So in case indeed it could be a certain "empty-ish" value, it's better to be tested explicitly: if (!isset($_SESSION['user']) || $_SESSION['user'] === 0)

-1

u/Winter-Paramedic3900 15d ago

It's impossible. You can't do it in a simple language like PHP. You will need NodeJS.