Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8984103
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T21:00:38+00:00 2026-06-15T21:00:38+00:00

I started creating a login system which utilised cookies for a remember me feature.

  • 0

I started creating a login system which utilised cookies for a “remember me” feature. All is working fine however I am having trouble deleting the cookie upon user logout.

If a user does not check the “remember me” box and logs in successfully I.e. does not create the cookie, the logout function works as expected and loads the login box.

If they don’t do the latter and the user clicks the logout button the cookie remains and it shows they are still logged in.

If someone could shine some light as to why the cookie wont delete I would be very grateful.

Below is the code I am using:

PHP code that runs after a user tries to log in:

// If the form has been submitted
if(isset($_POST['login'])):

    // Protect from unwanted code/string context
    $username = strip_tags(addslashes(trim($_POST['username'])));
    $string = strip_tags(addslashes(trim($_POST['password'])));
    $remember = strip_tags(addslashes(trim($_POST['remember'])));

    // Pass the returned variables from functions to a local versions
    $password = salting($string);   // Salt Password Preperation
    $link = db_connect();           // DB connection

    // Connect to the database and try to find a login match
    $result = mysqli_query($link,"SELECT * FROM web_users WHERE username='".$username."' AND password='".$password."'");
    $row    = mysqli_fetch_object($result);

    // Create erronous results if submitted data is invalid
    if (mysqli_num_rows($result) !== 1):
        $errmsg[0] = "Invalid Username or Password, please re-try";
    endif;

    $e_login = serialize($errmsg);

    // If validation passes then continue
    if (!$errmsg):
        // Increment the login_count field by 1
        $row->login_count++;
        $count = $row->login_count;

        // Retrieve the date for admin purposes
        $date = date('Y-m-d-h:i:s'); // Y=year (4 digits) m=month (leading zero) h=hour i=minutes s=seconds

        // Salt Password Preperation
        $string = session_id();
        $login_id = salting($string);

        // Connect to the database and update the related row
        $update = mysqli_query($link,"UPDATE web_users
                                      SET login_count='".$count."',
                                          login_last='".$date."',
                                          login_id='".$login_id."',
                                          logged='1'
                                      WHERE id='".$row->id."'")

                                      or die(mysqli_error($link));

        // Create a multi-dimensional session array
        $_SESSION['login'] = array('user'       => $row->display_name,
                                   'id'         => $row->id,
                                   'user_level' => $row->user_level);

        if($remember == 1):
            setcookie("login_user",session_id(),time() + (86400*7)); // 604800 = 1 week
        endif;

        // Free the memory and close the connection
        mysqli_free_result($result);
        mysqli_close($link);

        // Take the user to the successive page if no errors
        header("location: /");
    endif;
endif;

HTML code to create the logout element:

<a href="/logout" title="Logout">
    <img src="<? echo ASSETS . IMAGES . ICONS . GENERAL; ?>logout.png" alt="User Logout">
</a>

PHP code that runs when a user logs out:

function logout() {
    // Load the db connect function to pass the link var
    $link = db_connect();

    if(is_array($_SESSION['login'])):
        // Update the logged field to show user as logged out
        $update = mysqli_query($link,"UPDATE web_users SET logged='0' WHERE id='".$_SESSION['login']['id']."'") or die(mysqli_error($link));

        // Free the memory and close the connection
        mysqli_free_result($update);
        mysqli_close($link);

        // Unset all of the session variables.
        $_SESSION = array();

        // If it's desired to kill the session, also delete the session cookie.
        // Note: This will destroy the session, and not just the session data!
        if(isset($_COOKIE[session_name()])):
            setcookie(session_name(), '', time()-7000000, '/');
        endif;

        // Finally, destroy the session.
        session_destroy();

        // Take the user to the successive page if no errors
        header("location: /");
    endif;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T21:00:39+00:00Added an answer on June 15, 2026 at 9:00 pm

    The user, when logged in with the remember me checkbox to your site, will have two cookies. The session cookie, by default PHPSESSID, and the remember me cookie, login_user. In order to remove the session, you just remove the sesion cookie with this code:

        if(isset($_COOKIE[session_name()])):
            setcookie(session_name(), '', time()-7000000, '/');
        endif;
    

    The issue is that, aside from that, you need to unset the remember me cookie, with the following code.

        if(isset($_COOKIE['login_user'])):
            setcookie('login_user', '', time()-7000000, '/');
        endif;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been working on a dynamic question-and-answers system, but I'm having trouble creating a
I have an application that suddenly started throwing the following exception: System.Data.SqlClient.SqlException: Login failed
I have started using windows form for creating a small App in which user's
I started creating file downloader class. Here is what i have: public class Downloader
I have not started creating the code so I don't have anything to show,
I have started creating a game, and I added a second project, it's the
I have recently started creating an iPhone application using Appcelerator's Titanium. Since the application
I've been developing in Node.js for only 2 weeks and started re-creating a web
I wanted to create jquery plugin & started off creating a sample jquery plugin...But
I started using Core Data for iPhone development. I started out by creating a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.