Arjen
Arjen

Reputation: 87

html/php dropdown not working correctly

<li class="dropdown">
  <a href="signin.php" class="dropbtn">Account</a>
  <div class="dropdown-content">
  <?php
    if(isset($_SESSION['on'])) {
      echo '<a href="logout.php">Log out</a>';
    }
    else if (!isset($_SESSION['on'])) {
      echo '<a href="signin.php">Sign in</a>';
      echo '<a href="signup.php">Sign up</a>';
    }
  ?>
  </div>
</li>

So what this is supposed to do is when ur not signed in u get to see the signin and signup dropdown buttons and not the logout. This part works. But as soon as u log in, in stead of showing the log out button and nothing else, it only shows the sign in and sign up buttons. How do i fix this?

Upvotes: 2

Views: 118

Answers (1)

Jonny
Jonny

Reputation: 1329

session_start needs to be available to start or get a session, this needs to be placed at the beginning of your php on the page. If you are checking isset the else is already the result of what will happen if session is not set, so i removed

else if (!isset($_SESSION['on'])) 

and replaced it with a simple else.

session_start();
if(isset($_SESSION['on'])) {
  echo '<a href="logout.php">Log out</a>';
} else {
  echo '<a href="signin.php">Sign in</a>';
  echo '<a href="signup.php">Sign up</a>';
}

Remember to go back and use the session_start() on the page where you set the sessions to begin with.

Upvotes: 2

Related Questions