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
1 Answer
Reset to default 0Based 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
版权声明:本文标题:php - Check if user had autologin & if so, logout 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741330810a2372756.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论