3

I'm trying to remove some cookies set to control persistent login when the user specifically wants to log out of a website.

I thought it would be as simple as:

function remove_user_cookie() {

    unset($_COOKIE['woai']);
    unset($_COOKIE['woak']);

}
add_action('wp_logout', 'remove_user_cookie');

I've also tried:

function remove_user_cookie() {

    if( $_GET['action'] == 'logout' ) :

        unset($_COOKIE['woai']);
        unset($_COOKIE['woak']);

    endif;

}
add_action('wp_logout', 'remove_user_cookie');

However, the users cookies remain in place, so they keep getting logged back in automatically.

I'm not sure if the wp_logout action just isn't working, or if it's clashing with my code that logs the user in automatically.

Right now, I have this in the header.php:

<?php if( !is_user_logged_in() ) :

    // check if user has cookies
    if ( isset($_COOKIE['woai']) && isset($_COOKIE['woak']) ) :

        $args = array(
            'meta_key' => 'login_key',
            'meta_value' => $_COOKIE['woak'],
            'meta_compare' => '=',
            'fields' => 'ID',
            'include' => $_COOKIE['woai']
        );
        $user_query = new WP_User_Query($args);

        if( $user_query->results[0] != '' || $user_query->results[0] != NULL ) :

            // get user id and login name
            $user_id = $user_query->results[0];
            $user_login = get_user_by( 'id', $user_id );

            // log the user in
            wp_set_current_user( $user_id, $user_login->user_login );
            wp_set_auth_cookie( $user_id );

            do_action( 'wp_login', $user_login->user_login );

        endif;

    endif; // end if cookies

endif; ?>

And when the user is logged in, they get a new key set with this:

function set_user_cookie($user_login) {

    // if they have cookies, delete them
    if ( isset($_COOKIE['woai']) && isset($_COOKIE['woak']) ) :
        unset($_COOKIE['woai']);
        unset($_COOKIE['woak']);
    endif;

    $user_id = get_user_by( 'login', $user_login );

    // generate new key for user
    $salt = wp_generate_password(20); // 20 character "random" string
    $key = sha1($salt . $email . uniqid(time(), true));

    // set new cookies
    setcookie("woai", $user_id->ID, time()+31536000);  /* expire in 1 year */
    setcookie("woak", $key, time()+31536000);  /* expire in 1 year */

    // update the db
    update_field('field_53c3de9cd031e', $key, 'user_'.$user_id->ID.'');

}
add_action('wp_login', 'set_user_cookie', 10, 1);
1
  • Rather than using unset($_COOKIE['woai']), did you tried setcookie("woak", '', time() - 3600 ); ?
    – Shazzad
    Commented Jul 15, 2014 at 11:01

2 Answers 2

1

pls try below code

function remove_user_cookie() {

setcookie("woak");
setcookie("woai");

}
add_action('wp_logout', 'remove_user_cookie');

apart why you use cookie to storing some value ? suggest you to use session

1
  • This didn't work I'm afraid. I'm using cookies because this is for a persistent login, so the users need to keep a cookie so I know if they need to be auto-logged in or not :)
    – lukeseager
    Commented Jul 16, 2014 at 9:40
0

(I created a persistent login for my client, one login for two wordpress based site, on prestashop and 3 custom php application). I had the same challenge but this is the way i solved it.

First step

add_action('wp_logout', 'doMyLogoutCall');
add_action('init', 'runInitLogoutProcess');

second step

function doMyLogoutCall()
{?>
 <img style="display:none" src="<?php echo home_url();
    ?>?loadwordpressbyimage=1">
<?php
}

function runInitLogoutProcess()
{
  // check if wordpress action is set to logout and clear cookie
  if (isset($_GET)) {
        if (isset($_GET['action'])) {
            if ($_GET['action'] == 'logout') {
                setcookie('woai', '', time() - 3600, COOKIEPATH, COOKIE_DOMAIN, false);
               setcookie('woak', '', time() - 3600, COOKIEPATH, COOKIE_DOMAIN, false);
            }
        }

  // check if your wp_logout hook is called and do the same.
      if (isset($_GET['loadwordpressbyimage'])) {
            if ($_GET['loadwordpressbyimage'] == 1) {
                setcookie('woak', '', time() - 3600, COOKIEPATH, COOKIE_DOMAIN, false);
               setcookie('woai', '', time() - 3600, COOKIEPATH, COOKIE_DOMAIN, false);
            }
        }


} 

//That's it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.