Last active
October 12, 2020 21:54
-
-
Save rochow/7c10c0b6d265f4463d1a5064be525016 to your computer and use it in GitHub Desktop.
WordPress Login authentication requires same IP + Browser
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
add_action( 'wp_login', 'set_user_agent', 10, 2 ); | |
add_action( 'init', 'check_user_agent', 1 ); | |
function set_user_agent( $user_login, $user ) { | |
$user_agent = sanitize_text_field( $_SERVER['HTTP_USER_AGENT'] ); | |
$user_ip = sanitize_text_field( $_SERVER['REMOTE_ADDR'] ); | |
update_user_meta( $user->ID, 'user_agent', $user_agent ); | |
update_user_meta( $user->ID, 'user_ip', $user_ip ); | |
} | |
function check_user_agent() { | |
if( !is_user_logged_in() ) { | |
return; | |
} | |
$user_agent = sanitize_text_field( $_SERVER['HTTP_USER_AGENT'] ); | |
$set_user_agent = get_user_meta( get_current_user_id(), 'user_agent', true ); | |
$user_ip = sanitize_text_field( $_SERVER['REMOTE_ADDR'] ); | |
$set_user_ip = get_user_meta( get_current_user_id(), 'user_ip', true ); | |
if( $user_agent != $set_user_agent || $user_ip != $set_user_ip ) { | |
wp_logout(); | |
wp_redirect( site_url() ); | |
exit; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment