i'm kinda new in PHP and I have some issues. When I'm logged in on my website I want to hide the 'Login' and 'Register' button, and when I'm logged out I want to hide the 'logout' button.
This is my login.php page:
<?php
include "navbar.php";
?>
<?php
include_once "check.php";
$error = ""; //Variable for storing our errors.
if(isset($_POST["submit"]))
{
if(empty($_POST["username"]) || empty($_POST["password"]))
{
$error = "Both fields are required.";
}else
{
// Define $username and $password
$username=$_POST['username'];
$password=$_POST['password'];
// To protect from MySQL injection
$username = stripslashes($username);
$password = stripslashes($password);
$username = mysqli_real_escape_string($db, $username);
$password = mysqli_real_escape_string($db, $password);
$password = md5($password);
//Check username and password from database
$sql="SELECT uid FROM users WHERE username='$username' and password='$password'";
$result=mysqli_query($db,$sql);
$row=mysqli_fetch_array($result,MYSQLI_ASSOC);
//If username and password exist in our database then create a session.
//Otherwise echo error.
if(mysqli_num_rows($result) == 1)
{
$_SESSION['username'] = $username;
header("location: index.php");
}else
{
$error = "Incorrect username or password.";
}
}
}
?>
<html>
<head>
<link rel="stylesheet" type="text/css" href="assets/css/acc.css">
<link rel="stylesheet" type="text/css" href="assets/css/acc2.css">
<link rel="stylesheet" type="text/css" href="assets/css/navbar.css">
<title>Login | Peerbolte-ICT</title>
</head>
</body>
<section>
<p style="color:red;"><?php echo $error; ?> </p>
<form method="POST" action="">
<div class="accPanel">
<h1 style="color: black; padding-bottom: 5%;">Login</h1>
<form method="POST" action="">
<div class="login-input">
<label class="accLabel"><b>Username<span style="color:red;">*</span></b></label>
<input type="text" placeholder="Enter Username" name="username" required>
<label class="accLabel"><b>Password<span style="color:red;">*</span></b></label>
<input type="password" placeholder="Enter Password" name="password" required>
<button class="submitButton" type="submit" name="submit">Login</button>
</div>
</form>
</div>
</section>
</body>
</html>
And this is my navbar.php where the buttons are:
<ul>
<li><a href="index.php">Home</a></li>
<li><a href="#bannerTwo">Services</a></li>
<li class="dropdown">
<a href="javascript:void(0)" class="dropbtn">Our Work</a>
<div class="dropdown-content">
<a href="#">Link 1</a>
<a href="#">Link 2</a>
<a href="#">Link 3</a>
</div>
</li>
<li class="dropdown">
<a href="javascript:void(0)" class="dropbtn">Account</a>
<div class="dropdown-content">
<a href="login.php">Login</a>
<a href="register.php">Register</a>
<a href="logout.php">Logout</a>
</div>
</li>
</ul>
Thanks
you can display your link based on condition. check session for that if user login you get session value on base of that logic you can put condition like below:
<?php if( isset($_SESSION['username']) && !empty($_SESSION['username']) )
{
?>
<a href="logout.php">Logout</a>
<?php }else{ ?>
<a href="login.php">Login</a>
<a href="register.php">Register</a>
<?php } ?>
Note: make your you start session in file.
You can use sessions to check if a user is logged in or logged out.
$_SESSION['status'] = 1;//if logged in
$_SESSION['status'] = 0;//if logged out
To hide the links, you can use this:
<a href="login.php" <?php echo ($_SESSION['status'] == 1) ? 'style="display:none;"' : '' ?> >Login</a>
<a href="register.php" <?php echo ($_SESSION['status'] == 1) ? 'style="display:none;"' : '' ?>>Register</a>
<a href="logout.php" <?php echo ($_SESSION['status'] == 0) ? 'style="display:none;"' : '' ?>>Logout</a>
And don't forget to the put session_start() so that you can use sessions
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With