Login User
$user = get_user_by( 'id', $user_id );
if ($user->ID > 0)
{
if (is_user_logged_in())
wp_logout();
wp_set_current_user($user_id, $user->user_login); //This setting the current user does not log in that user. We also need the 2 following lines
wp_set_auth_cookie($user_id);
do_action('wp_login', $user->user_login);
}
Logout User
if (is_user_logged_in())
wp_logout();
Feel free to comment if you can add help to this page or point out issues and solutions you have found. I do not provide support on this site, if you need help with a problem head over to stack overflow.