admin管理员组

文章数量:1287915

I'm trying to create a function which autologins users, but upon visiting certain pages also performs an autologout if the initial login was obtained via autologin.

I've written the following code but I guess my boolean check for the autologin boolean does not work.

/* AUTO LOGIN / LOGOUT */
function autologinout() {
    global $wp;

    // Autologin
    if( isset($_GET['username']) ) {
        $user = get_user_by('login', $_GET['username']);

        // Redirect URL //
        if ( !is_wp_error( $user ) ) {
            if ( in_array( 'customer', (array) $user->roles ) ) {
                wp_clear_auth_cookie();
                wp_set_current_user ( $user->ID );
                wp_set_auth_cookie  ( $user->ID );
                wp_redirect( '/lovelists/toon-lovelist/' );
                $autologin = true;
                exit();
            }
        }
    }

    // Autologout
    $path = $_SERVER['REQUEST_URI'];
    if ( is_user_logged_in() && $autologin = true  && ( $path == '/lovelists/maak-lovelist/' || $path == '/lovelists/login/' ) ) {
        wp_clear_auth_cookie(); // so you don't get the cache error
        wp_logout(); // this will logout user
        $autologin = false;
    }
}

add_action( 'init', 'autologinout' );

Any idea what I'm doing wrong?

I'm trying to create a function which autologins users, but upon visiting certain pages also performs an autologout if the initial login was obtained via autologin.

I've written the following code but I guess my boolean check for the autologin boolean does not work.

/* AUTO LOGIN / LOGOUT */
function autologinout() {
    global $wp;

    // Autologin
    if( isset($_GET['username']) ) {
        $user = get_user_by('login', $_GET['username']);

        // Redirect URL //
        if ( !is_wp_error( $user ) ) {
            if ( in_array( 'customer', (array) $user->roles ) ) {
                wp_clear_auth_cookie();
                wp_set_current_user ( $user->ID );
                wp_set_auth_cookie  ( $user->ID );
                wp_redirect( '/lovelists/toon-lovelist/' );
                $autologin = true;
                exit();
            }
        }
    }

    // Autologout
    $path = $_SERVER['REQUEST_URI'];
    if ( is_user_logged_in() && $autologin = true  && ( $path == '/lovelists/maak-lovelist/' || $path == '/lovelists/login/' ) ) {
        wp_clear_auth_cookie(); // so you don't get the cache error
        wp_logout(); // this will logout user
        $autologin = false;
    }
}

add_action( 'init', 'autologinout' );

Any idea what I'm doing wrong?

Share Improve this question asked Sep 8, 2021 at 13:37 BarrieOBarrieO 992 silver badges12 bronze badges 2
  • 1 When you set a variable that only sets the variable for the current request/page. When you visit another page it's gone. If you want data to persist across requests you need to store it in a cookie, local storage, or the database. In your case you would probably want to store the autologin status as a cookie. – Jacob Peattie Commented Sep 8, 2021 at 15:16
  • @JacobPeattie that’s insightful. Thank you for your time & help. I will try that tomorrow! – BarrieO Commented Sep 8, 2021 at 17:37
Add a comment  | 

1 Answer 1

Reset to default 0

Based on the suggestion of @JacobPeattie to use a cookie, I've constructed following working solution for this question:

/* LOGIN / LOGOUT */
/* --- */
/* AUTOLOGIN */
if( isset($_GET['username']) ) {
    $user = get_user_by('login', $_GET['username']);

    // Redirect URL //
    if ( !is_wp_error( $user ) ) {
        if ( in_array( 'customer', (array) $user->roles ) ) {
            wp_clear_auth_cookie();
            wp_set_current_user ( $user->ID );
            wp_set_auth_cookie  ( $user->ID );

            wp_redirect( '/lovelists/toon-lovelist/' );

            $autologin = 1;
            setcookie( 'autologin_status', $autologin, time()+31556926 , "/" );
            exit();
        }
    }
}

/* AUTOLOGOUT */
function log_out_user() {
global $wp;
$path = $_SERVER['REQUEST_URI'];
    if ( is_user_logged_in() && $_COOKIE['autologin_status'] == 1 && ( $path == '/lovelists/maak-lovelist/' || $path == '/lovelists/login/' ) ) {
        wp_logout(); // this will logout user
        unset( $_COOKIE['autologin_status'] );
    }
}

add_action( 'init', 'log_out_user' );

本文标签: phpCheck if user had autologin amp if sologout