So I was recently started working on a login and register page. After doing all the necessary connections and such, I realized my logout button wasn't working.
Not sure exactly why the button isn't working, because I'm not getting any error message when I click it.
I tried using different Session Handles thinking it might work.
<?php
session_start();
unset($_SESSION['id']);
session_destroy(); // Destroying All Sessions
header("Location: ../index.php"); // Redirecting To Home Page
?>
this is my logout.inc.php
That didn't change anything, so I thought it was something to do with how I structured my form actions.
<?php
if (!isset($_SESSION['id'])) {
echo '<form action="includes/logout.inc.php" method="post">
<button type="submit" name="logout-submit">Logout</button>
</form> ';
}
else if (isset($_SESSION['id'])) {
echo '<form action="includes/login.inc.php" method="post">
<input type="text" name="mailuid" placeholder="E-mail/Username">
<input type="password" name="pwd" placeholder="Password">
<button type="submit" name="login-submit">Login</button>
</form>
<a href="signup.php" class="header-signup">Signup</a>';
}
?>
I'm presuming, the problem is something to do with my login script
<?php
if (isset($_POST['login-submit'])) {
require 'dbh.inc.php';
$mailuid = $_POST['mailuid'];
$password = $_POST['pwd'];
if (empty($mailuid) || empty($password)) {
header("Location: ../index.php?error=emptyfields");
exit();
}
else {
$sql = "SELECT * FROM users where uidUsers=? OR emailUsers=?;";
$stmt = mysqli_stmt_init($conn);
if (!mysqli_stmt_prepare($stmt, $sql)){
header("Location: ../index.php?error=sqlerror");
exit();
}
else {
mysqli_stmt_bind_param($stmt, "ss", $mailuid, $mailuid);
mysqli_stmt_execute($stmt);
$result = mysqli_stmt_get_result($stmt);
if ($row = mysqli_fetch_assoc($result)) {
$pwdCheck = password_verify($password, $row['pwdUsers']);
if ($pwdCheck == false) {
header("Location: ../index.php?error=wrongpassword");
exit();
}
else if ($pwdCheck == true) {
session_start();
$_SESSION['userId'] = $row['idUsers'];
$_SESSION['userUid'] = $row['uidUsers'];
header("Location: ../index.php?login=success");
exit();
}
else {
header("Location: ../index.php?error=wrongpassword");
exit();
}
}
else {
header("Location: ../index.php?error=nouser");
exit();
}
}
}
}
else {
header("Location: ../index.php");
exit();
}
?>
But even then, I don't know where to start so I was hoping someone could help find something I missed or something that needs to be changed.
My expected output is that the logout button when clicked, will completely terminate the current session and start a new one, which in turn will transfer me to the login/sign up page.
<?php
require "header.php";
?>
<main>
<div class="wrapper-main">
<section class="section-default">
<!--
We can choose whether or not to show ANY content on our pages depending on if we are logged in or not.
-->
<?php
if (!isset($_SESSION['userId'])) {
echo '<p class="login-status">You have Successfully logged In!</p>';
}
else if (isset($_SESSION['userId'])) {
echo '<p class="login-status">You are logged Out!</p>';
}
?>
</section>
</div>
</main>
<?php
// And just like we include the header from a separate file, we do the same with the footer.
require "footer.php";
?>
this is the index.php file