hello,

my sign in working fine..but not my logout and signin script...help please.

this is my logout script:
<?php
session_start();
session_unset();
session_destroy();
header("Location: index.php");

and my signin 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=?; ";
        $stmt = mysqli_stmt_init($conn);

        if (!mysqli_stmt_prepare($stmt, $sql)) {
            header("Location: ../index.php?error=sqlerror");
            exit();
        }
        else{
            mysqli_stmt_bind_param($stmt, "s", $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 == flase) {
                    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();
}

Dani AI

Generated

Most likely causes (from 's snippets): a simple typo in the password check, inconsistent redirect paths, and session cookies not being cleared. 's request for "what exactly is not working?" is on point — useful debug steps are listed below. 's suggestion to use a full URL will work, but is usually unnecessary if paths are consistent.

A few concrete fixes to apply:

  • Fix the typo flase -> false (or better: test password_verify with a negation).
  • Make all redirects use the correct relative/absolute path (errors use ../index.php so success should too, or use a root path like /index.php).
  • Always call exit() immediately after a header("Location: ...").
  • When logging out, clear the session cookie as well as server-side session data.

Example: a minimal, robust password check and redirect (replace paths to match the script location):

// after fetching $row
if (!password_verify($password, $row['pwdUsers'])) {
    header("Location: ../index.php?error=wrongpassword");
    exit();
}
session_start();
$_SESSION['userId'] = $row['idUsers'];
$_SESSION['userUid'] = $row['uidUsers'];
header("Location: ../index.php?login=success");
exit();

Example: a more reliable logout sequence that removes the cookie before destroying the session:

session_start();
$_SESSION = array();
if (ini_get('session.use_cookies')) {
    $p = session_get_cookie_params();
    setcookie(session_name(), '', time() - 42000, $p['path'], $p['domain'], $p['secure'], $p['httponly']);
}
session_destroy();
header("Location: ../index.php");
exit();

Quick troubleshooting checklist:

  • Look for "headers already sent" errors — check for BOM or stray whitespace before <?php.
  • Use the browser Network tab to confirm the server sends the expected Location header and status (3xx).
  • Enable error reporting during development (error_reporting(E_ALL); ini_set('display_errors', 1);) and check server logs.
  • Ensure pages that check login state call session_start() at the top.

Applying those fixes typically resolves both logout and redirect oddities without needing full absolute URLs.

Recommended Answers

All 2 Replies

What exactly is not working?

You need to use full path

   <?php
   session_start();
     session_unset();
     session_destroy();
     header("Location: https://www.google.com/page_name.php");
    ?>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.