2
<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?

7
  • 2
    Your else if is totally redundant; just use else. Have you called session_start() before this (or any other) HTML output? Commented May 24, 2018 at 0:20
  • on the log in page u start the session. And yes the else if i get why it's redundant. I was trying so many things to try and make it work. Do i need to start the session on every page? i thought when u start session once then u have started it until u logout Commented May 24, 2018 at 0:23
  • How do you "login"? Does it cause this page to be reloaded? Commented May 24, 2018 at 0:23
  • justforkicks.000webhostapp.com/index.php that's the link. I don't know how to explain it because i just started with coding Commented May 24, 2018 at 0:26
  • 1
    I suggest you actually read the documentation ~ "session_start() creates a session or resumes the current one" Commented May 24, 2018 at 0:27

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.