Allow login from specific IP Address


/ Published in: PHP
Save to your folder(s)

Allow login from specific IP Address


Copy this code and paste it in your HTML
  1. // Allow login from specific IP Address
  2. function get_the_user_ip() {
  3. if ( ! empty( $_SERVER['HTTP_CLIENT_IP'] ) ) {
  4. //check ip from share internet
  5. $ip = $_SERVER['HTTP_CLIENT_IP'];
  6. } elseif ( ! empty( $_SERVER['HTTP_X_FORWARDED_FOR'] ) ) {
  7. //to check ip is pass from proxy
  8. $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
  9. } else {
  10. $ip = $_SERVER['REMOTE_ADDR'];
  11. }
  12. return apply_filters( 'wpb_get_ip', $ip );
  13. }
  14.  
  15. add_shortcode('show_ip', 'get_the_user_ip');
  16.  
  17. add_filter( 'authenticate', 'my_custom_authenticate', 10, 3 );
  18. function my_custom_authenticate(){
  19. //Get POSTED value
  20. if(isset($_POST['log']) && !empty($_POST['log'])){
  21.  
  22. $username='';
  23. $username=$_POST['log'];
  24. $admin_ip='000.000.00.00'; // replace your IP here
  25. $user_id=2;
  26. $current_ip=do_shortcode('[show_ip]');
  27. //Get user object
  28. $user = get_user_by('login',$username);
  29.  
  30. if($user_id==$user->ID){
  31. if($admin_ip != $current_ip){
  32. add_filter('login_errors','login_error_message');
  33.  
  34. function login_error_message($error){
  35.  
  36. //check if that's the error you are looking for
  37. $pos = strpos($error, 'incorrect');
  38. if (is_int($pos)) {
  39. //its the right error so you can overwrite it
  40. $error = "<strong>ERROR</strong>: You're unique identifier was invalid.";
  41. }
  42. return $error;
  43. }
  44. //User note found, or no value entered or doesn't match stored value - don't proceed.
  45. remove_action('authenticate', 'wp_authenticate_username_password', 20);
  46.  
  47. }
  48. }
  49.  
  50. //Make sure you return null
  51. return null;
  52. }
  53. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.