-1

I am working on changing the "Login!" button to the "Logout" button when the user signs in to the website. For some reason, the login button is showing as default even after logging in.

Here is my header part which shows the PHP statement for the buttons

    <?php session_start(); ?>

    <!DOCTYPE html>
    <html>

    <head>
      <meta name="viewport" content="width=device-width, initial scale=1.0" />

      <link rel="preconnect" href="https://fonts.googleapis.com">
      <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
      <link href="https://fonts.googleapis.com/css2?family=Nunito+Sans:wght@400;600;700;800&display=swap" rel="stylesheet">
      <link href="https://fonts.googleapis.com/css2?family=Noto+Sans:ital@0;1&display=swap" rel="stylesheet">

      <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.13.0/css/all.min.css">


    </head>

    <style>
      h1 {
        font-family: 'Nunito Sans', sans-serif;
        font-weight: 700;
      }
    </style>

    <body>

      <nav class="navbar sticky-top navbar-expand-md navbar-dark bg-primary flex-wrap">
        <div class="container-fluid">

          <a class="navbar-brand" href="index.php">
            <h1>The Demo Project</h1>
          </a>

          <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarheader" aria-controls="navbarheader" aria-expanded="false" aria-label="Toggle navigation">
                    <span class="navbar-toggler-icon"></span>
                </button>

          <div class="collapse navbar-collapse" id="navbarheader">

            <form class="col-12 d-xs-block d-md-none py-2">
              <input type="search" class="form-control" placeholder="Search..." aria-label="Search">
            </form>

            <ul class="navbar-nav me-auto mb-2 mb-md-0 mx-0 text-center">
              <li class="nav-item">
                <a class="nav-link" aria-current="page" href="index.php">Home</a>
              </li>
              <li class="nav-item">
                <a class="nav-link" href="post.php">Post</a>
              </li>
              <li class="nav-item">
                <a class="nav-link" href="stories.php">Stories</a>
              </li>
              <li class="nav-item dropdown">
                <a class="nav-link dropdown-toggle" href="#" id="dropdownitems" data-bs-toggle="dropdown" aria-expanded="false">Resources</a>
                <ul class="dropdown-menu" aria-labelledby="dropdownitems">
                  <li><a class="dropdown-item" href="organizations.php">All Organizations</a></li>
                  <li><a class="dropdown-item" href="health.php">Health Updates</a></li>
                  <li><a class="dropdown-item" href="test.php">Data Test</a></li>
                </ul>
            </ul>

            <form class="col-12 col-md-4 col-lg-auto me-md-3 d-none d-md-block">
              <input type="search" class="form-control" placeholder="Buscar..." aria-label="Search">
            </form>

            <div class="d-grid gap-2 d-md-flex justify-content-md-end pb-md-0 pb-2 px-md-0">

              <?php
                            if (isset($_SESSION['email'])) {
                                echo " <a role='button' class='btn btn-outline-light' href='loginpage.php'>Login!</a> ";
                                echo " <a role='button' class='btn btn-info' href='register.php'>Signup</a> ";
                            }
                        
                            else {
                                echo " <a role='button' class='btn btn-outline-light' href='logout.php'>Logout</a> ";
                                echo " <a role='button' class='btn btn-info' href='register.php'>Signup</a> ";
                            }
                        
                        ?>
            </div>

          </div>

        </div>
      </nav>

      <!-- 
       <div class="d-grid gap-2 d-md-flex justify-content-md-end pb-md-0 pb-2 px-md-0">

        <a role="button" class="btn btn-outline-light" href="loginpage.php">Login</a>

        <a role="button" class="btn btn-info" href="register.php">Signup</a>
    </div>



    ($_SESSION['email'])
    -->


    </body>

    </html>

Specifically this part:

<div class="d-grid gap-2 d-md-flex justify-content-md-end pb-md-0 pb-2 px-md-0">

    <?php
        if (isset($_SESSION['email'])) {
          echo " <a role='button' class='btn btn-outline-light' 
          href='loginpage.php'>Login!</a> ";
          echo " <a role='button' class='btn btn-info' 
          href='register.php'>Signup</a> ";
        }
                        
        else {
          echo " <a role='button' class='btn btn-outline-light' 
          href='logout.php'>Logout</a> ";
          echo " <a role='button' class='btn btn-info' 
          href='register.php'>Signup</a> ";
       }
                        
     ?>

</div>

Here is my login.php file for additional reference

<?php
// Check if the form has been submitted
$email = $_POST['email'];
$pass = $_POST['pass'];
$errors = []; //Initialize error array

if (empty($email)) {
    $errors[] = 'You forgot to enter your email address';
}

if (empty($pass)) {
    $errors[] = 'You forgot to enter your password';
}

if (!empty($email) && !empty($pass)){
    require_once('includes/mysqli_connect.php');
    $q = "SELECT * from users WHERE email='$email' AND pass=SHA2('$pass', 512)";
    $result = @mysqli_query($dbc,$q); //Run the query
    $row= mysqli_fetch_array($result, MYSQLI_ASSOC);
    if($row){
        session_start();
        $_SESSION['id'] = $row['id'];
        $_SESSION['user_name'] = $row['user_name'];
        $_SESSION['email'] = $row['email'];
        //Redirect
        header("Location:welcome.php");
        
        //exit(); //Quit the script
    }
}

$errors[] = 'Your email address and password do not match!'; //Errors if there is incorrect email/password
if (!empty($errors)){
    echo '<h1>ERROR!</h1>
    <p>The following error(s) occured due to:<br>';
    foreach ($errors as $msg) {
        echo "$msg<br>";
    }
    echo '</p><p><a href=loginpage.php>Please try again.</a></p>';
}

Zee
  • 15
  • 3
  • **Never store passwords in clear text or using MD5/SHA1!** Only store password hashes created using PHP's [`password_hash()`](https://php.net/manual/en/function.password-hash.php), which you can then verify using [`password_verify()`](https://php.net/manual/en/function.password-verify.php). Take a look at this post: [How to use password_hash](https://stackoverflow.com/q/30279321/1839439) and learn more about [bcrypt & password hashing in PHP](https://stackoverflow.com/a/6337021/1839439) – Dharman Jul 28 '21 at 13:10

3 Answers3

0

I think you may have your logic slightly off in the if statement.

You have:

if (isset($_SESSION['email'])) {
      echo " <a role='button' class='btn btn-outline-light' 
      href='loginpage.php'>Login!</a> ";
      echo " <a role='button' class='btn btn-info' 
      href='register.php'>Signup</a> ";
    }
                    
    else {
      echo " <a role='button' class='btn btn-outline-light' 
      href='logout.php'>Logout</a> ";
      echo " <a role='button' class='btn btn-info' 
      href='register.php'>Signup</a> ";
   }

This is saying, if the session Email exists then show login else show logout.

Try:

if (!isset($_SESSION['email'])) {
  echo " <a role='button' class='btn btn-outline-light' 
  href='loginpage.php'>Login!</a> ";
  echo " <a role='button' class='btn btn-info' 
  href='register.php'>Signup</a> ";
}

else {
  echo " <a role='button' class='btn btn-outline-light' 
  href='logout.php'>Logout</a> ";
  echo " <a role='button' class='btn btn-info' 
  href='register.php'>Signup</a> ";
}

You can even move the Signup button out of the logic to keep your code DRY.

that would look like:

 <div class="d-grid gap-2 d-md-flex justify-content-md-end pb-md-0 pb-2 px-md-0">

          <?php
              if (!isset($_SESSION['email'])) {
                 echo " <a role='button' class='btn btn-outline-light' href='loginpage.php'>Login!</a> ";
              } else {
                 echo " <a role='button' class='btn btn-outline-light' href='logout.php'>Logout</a> ";
              }
           ?>

          <a role='button' class='btn btn-info' href='register.php'>Signup</a>
        </div>
Will T
  • 647
  • 5
  • 16
0

There is a problem in your condition.

Here, you display Login button when user is logged because you check if $_SESSION['email'] exists with isset function.

Add a ! before isset and remove Signup button when user is logged.

<?php
    if (!isset($_SESSION['email'])) {
      echo " <a role='button' class='btn btn-outline-light' href='loginpage.php'>Login!</a> ";
      echo " <a role='button' class='btn btn-info' href='register.php'>Signup</a> ";
    } else {
      echo " <a role='button' class='btn btn-outline-light' href='logout.php'>Logout</a> ";
   }
                    
 ?>
ThomasHWeb
  • 532
  • 1
  • 4
  • 10
-1

Add theis code in header file.

<?php
if (isset($_SESSION['email']))
{ echo " <a role='button' class='btn btn-outline-light' href='logout.php'>Logout</a> ";echo " <a role='button' class='btn btn-info href='register.php'>Signup</a> ";}
else{   echo " <a role='button' class='btn btn-outline-light' href='loginpage.php'>Login!</a> ";    echo " <a role='button' class='btn btn-info' href='register.php'>Signup</a> ";}?>
Remees M Syde
  • 2,564
  • 1
  • 19
  • 42
sanjay
  • 1
  • 1