I have a Maybe you want to try this instead, it worked for me. Try this. I don't how to explain but when the login is successful I created the session variable. If the session variable is not in used it will echo the Login that will link to login page otherwise if session variable is being used it will print the username code in process of logging in code in navbar In your And else, instead of checking the value of a $_SESSION and determining the value of it, you can use the following: Other options are storing variables in a I hope this has helped you out If you want the username to be displayed instead like in the example you need to change your code for onlinestore.html to the following: Either you need to rename your In your In your If the above doesn't work then please tell me what happens. Try this for debugging: In your This will show you if login is working. If it's not and you get "login failed" then that is why you get the "Login/Register" link on your page. If it shows "Login worked" then set the code back to how it was and then on your What happens? Do you get the message "This is working" on the page and nothing else?
<div class="msg"><?php echo $msg;?></div>
<form type="submit">
<input type="text" name="Email">
<input type="password" name="Password">
<button type="submit" name="Login">Login</button>
</form>
<?php
require 'connection.php';
if (isset($_POST['Login'])) {
$email = $_POST['Email'];
$password = $_POST['Password'];
$user = "SELECT username FROM table_name WHERE email = '".$email."' and password = '".$password."'";
$user = mysqli_query($con, $user);
if (mysqli_num_rows($user) > 0) {
while ($row = mysqli_fetch_array($user)) {
$username = $row['username'];
}
} else {
$msg = "Can't Log in";
}
}
echo $username;
?>
<?php
if($_SESSION['username'] == true) {
echo '<a href="logout.php"><span>Logout</span></a>';
}
else {
echo '<a href="login.php"><span>Login</span></a>';
}
?>
if($row['col_userName'] == $username AND $row['col_custPass'] = $password){
$_SESSION['session_var_user'] = $username; // I created the session variable also don't forget to put the session_start(); at the top of your code
echo "
<script>
window.location.href = 'index.php';
</script>
";
}else{echo "failed";}
<li class="nav-item">
<a class = "justtextstyle" href="#">
<?php
if(!isset($_SESSION['session_variable']))
// if(!isset($_SESSION['session_variable'])) checking if $_SESSION['session_variable' is in use; sorry idk to explain, it's just like that lol
{
echo "<a class = 'justtextstyle' href='login.php'>Log In Here</a>";
}
else
{
echo "Hi," . $_SESSION['session_variable'];
}
?>
</a>
</li>
else statement you haven't defined a session_start() like you did in your if statement.if (session_status() == PHP_SESSION_NONE) {
//Do something if the session is not existing
}
$_COOKIE and then check if it isset or not(if(isset($_COOKIE["username"]){})<li class='active' style='float:right;'>
<?php
session_start();
if($_SESSION['logged']==true){
echo '<a href="logout.php"><span>' + $_SESSION["username"] + ' (Logout)</span></a></li>';
}
elseif($_SESSION['logged']==false)
echo '<a href="registerform.html"><span>Login/Register</span></a></li>';
?>
onlinestore.html and login.html to be .php files so the PHP will work in them, or use the addtype option in your htaccess file. onlinestore.html do this: <?php
session_start(); // Right at the top of your script
?>
<li class='active' style='float:right;'>
<?php
if($_SESSION['logged']==true)
{
echo $_SESSION["username"];
echo '<a href="logout.php"><span>Logout</span></a></li>';
}
elseif($_SESSION['logged']==false)
{
echo '<a href="registerform.html"><span>Login/Register</span></a></li>';
}
?>
checklogin.php do this: <?php
session_start(); // Right at the top of your script
?>
if($count==1)
{
$_SESSION['logged']=true;
$_SESSION['username']=$myusername;
header("Location: onlinestore.html");
exit();
}
else
{
$_SESSION['logged']=false;
header("Location: login.html");
exit();
}
Do you have html files set to parse PHP code?
Or a htaccess file with:
AddType application/x-httpd-php .htm .html
? EDIT
checklogin.php do this: <?php
session_start(); // Right at the top of your script
?>
if($count==1)
{
$_SESSION['logged']=true;
$_SESSION['username']=$myusername;
echo "Login worked";
exit();
}
else
{
$_SESSION['logged']=false;
echo "Login failed";
exit();
}
onlinestore.html page, do this at the top of the file: echo "This is working";
exit();