<?php
/*
Plugin Name: User Login via IP
Plugin URI: http://blog.stefan-macke.de/2006/11/28/wordpress-login-via-ip-address/ 
Description: Automatically log in a user via his IP address.
Version: 1.0
Author: Stefan Macke
Author URI: http://blog.stefan-macke.de/
*/

add_action('plugins_loaded''useriplogin');

function 
useriplogin()
{
    
// leave function if user is already logged in or tries to log out
    
if (is_user_logged_in() || $_SERVER['PHP_SELF'] == 'wp-login.php')
    {
        return 
true;
    }
    
    
// get current IP
    
$ip $_SERVER['REMOTE_ADDR'];
    
    
// get database object and query the database for current IP
    
global $wpdb;
    
$result $wpdb->get_results('SELECT * FROM ' $wpdb->usermeta ' WHERE meta_key=\'ipaddress\' AND meta_value=\'' $ip '\' LIMIT 0, 1');
    
    
// IP address not found in database
    
if (!is_array($result))
    {
        return 
true;
    }

    
// read user from database
    
$user_id intval($result[0]->user_id);
    
$result $wpdb->get_results('SELECT user_login, user_pass FROM ' $wpdb->users ' WHERE ID=' $user_id ' LIMIT 0, 1');
    
// user not found in database
    
if (!is_array($result))
    {
        return 
false;
    }

    
$user_login $result[0]->user_login;
    
$user_pass $result[0]->user_pass;

    if (
wp_login($user_loginmd5($user_pass), true))
    {
        
wp_setcookie($user_loginmd5($user_pass), true''''false);
        
// reload the current page
        // this is needed, because login via cookie is performed before loading the plugin    
        
header('Location: ' $_SERVER['PHP_SELF']);
        exit;
    }
}

?>