Jim Trexaer
Jim Trexaer

Reputation: 3

show logout link in navigation bar when logged in php

When user logged in, the login link on nav bar should be gone and logout link should appear how should i do it?

index.html:

<nav>
    <p class="menu"><a href="index.html">Home</a></p> 
    <p class="menu"><a href="Product1.html">Products</a> </p> 
    <p class="menu"><a href="login.html">Login</a></p> 
    <p class="menu"><a href="logout.php">Logout</a></p> 
    </nav>

Login.php file:

<?php
require 'db.php';
session_start();

$password = $mysqli->escape_string($_POST['Pass']);
$email = $mysqli->escape_string($_POST['EmailAdd']);
$result = $mysqli->query("SELECT * FROM Account WHERE Usermail='$email'");

//check email in db
 if ($result->num_rows == 0)
 {
 $_SESSION['message'] = "Email does not exist";

 print '<script type="text/javascript">alert("' . $_SESSION['message'] . 
 '");
</script>';
header("Location: ../register.html");
}
else
{
//get user array
$user = $result->fetch_assoc();

if ($password == $user['password'])
{
    $box = "Login successful";
    $_SESSION['email'] = $user['Usermail'];
    $_SESSION['logged_in'] = true;

echo ("<SCRIPT LANGUAGE='JavaScript'>
window.alert('Login Successful')
window.location.href='../index.html';
</SCRIPT>");

}
else
{
    $_SESSION['message'] = "Wrong password";

    header("Location: ../account.html");

    echo "failed";
    echo '<script language="javascript">';
    echo 'alert("Wrong password")';
    echo '</script>';
   }
 }
 ?>

I've gone through some of the post in stack overflow and apply things like if (!isset($_SESSION['email'])) and else statement on my index.php but its not working and i don't know what's the prob

Ps Previously was using index.php, since its not working so i change it back to index.html

Upvotes: 0

Views: 4143

Answers (5)

Simon Gomes
Simon Gomes

Reputation: 383

This should work:

<nav>
  <p class="menu"><a href="index.html">Home</a></p> 
  <p class="menu"><a href="Product1.html">Products</a></p> 
  <p class="menu">
     <?php if(isset($_SESSION['logged_in]) && $_SESSION['logged_in]) {?>
        <a href="logout.php">Logout</a>
     <?php } else { ?>
        <a href="login.html">Login</a>
      <?php } ?>
  </p>
</nav>

Upvotes: 0

Mad Angle
Mad Angle

Reputation: 2330

<nav>
    <p class="menu"><a href="index.html">Home</a></p> 
    <p class="menu"><a href="Product1.html">Products</a> </p> 
    <?php if(empty($_SESSION['logged_in'])){ ?>
    <p class="menu"><a href="login.html">Login</a></p> 
    <?php } else{ ?>
    <p class="menu"><a href="logout.php">Logout</a></p> 
    <?php } ?>

</nav>

Upvotes: 0

S. Bos
S. Bos

Reputation: 68

Try this in your nav:

    <?php    
    if($_SESSION['logged_in'] == "true"){
    echo '<p class="menu"><a href="logout.php">Login</a></p>';
    }
    else {
    echo '<p class="menu"><a href="login.html">Logout</a></p>';
    }
    ?>

It is not working because you use the .html extension instead of .php

Upvotes: 0

Raman
Raman

Reputation: 644

 <?php 
if(!isset($_SESSION['logged_in'])){?>
    <p class="menu"><a href="login.html">Login</a></p> 
 <?php }

else
{?>    <p class="menu"><a href="logout.php">Logout</a></p> 
<?php } ?>

try the above code, Hope this helps

Upvotes: 3

Latheesan
Latheesan

Reputation: 24116

Assuming your code is correctly validating the credential and setting the auth state in the session $_SESSION['logged_in'] = true;

You can do something like this:

<nav>
    <p class="menu"><a href="index.html">Home</a></p> 
    <p class="menu"><a href="Product1.html">Products</a> </p> 
    <?php if (isset($_SESSION['logged_in']) && $_SESSION['logged_in'] == true): ?>
    <p class="menu"><a href="logout.php">Logout</a></p>
    <?php else: ?>
    <p class="menu"><a href="login.html">Login</a></p> 
    <?php endif; ?>
</nav>

Upvotes: 1

Related Questions