WordPress: Automatically login a user based on referring url


#1

So, I am working with a company who has a cloud based program with different clients who login to it. They created a WordPress help site and have configured this to only display certain content based on the user who is logged in to that. But, they no longer want a double login for the clients… one into their program and another into the help sections. So, I was thinking about an automatic login to a specific user in WP (not role, but actual user) based on referring url. Possible?


#2

This should do - http://cleverwp.com/autologin-wordpress-php-script/


#3

Just make sure you use an url complicated enough to prevent anyone from guessing the login url.


#4

Thanks guys. I think I actually have it figured out!


#5

Didnt think about this… I have that code linked above in plugin form, was all happy, and then realized a lot of the links that come into the site go to specific pages. If I use the URL login method, then I can make them land on the home page or something, however- I need a way to have them land in any number of places on the site… wonder what else it would take to click a url aiming to any specific page, log in, and continue to that page in one swoop… lol… Might be too much.


#6

I do this in one of my plugin demos to automatically log the user into the “demo” account:

function process_demo_login(){
        if(isset($_GET['do_login']) && !is_user_logged_in()){
            $user = wp_authenticate('demo', 'password');
            $secure_cookie = '';
            wp_set_auth_cookie($user->ID, true, $secure_cookie);
            do_action('wp_login', 'demo');
            wp_redirect(get_home_url());
        }
}
add_action('plugins_loaded','process_demo_login');

You could change the $_GET[‘do_login’] to check for referring url (note: some browsers don’t sent this url, especially those with certain internet security suites installed on their computer).

Then you could change the wp_redirect(get_home_url()); to something like wp_redirect($_SERVER[‘REQUEST_URI’]); to redirect them back to the page they originally wanted to see


#7

Awesome, I will test this today. That is exactly what I was hoping to find. Thank you so much.


#8

I also found this bit in an outdated plugin about friend url’s:

  
 if (!is_user_logged_in()) 
	{
		$aFriend[] = array('URL' => 'http://www.somesite.com', 'USER' => 'guest');
		
		$sReferer = strtolower($_SERVER['HTTP_REFERER']);
		
		
		$bAutoLogin = false;
		
		
		foreach($aFriend as $aConfig)
		{
			
			// Searching FriendURL in ReferralURL; :)
			$sReferer = substr($sReferer, 0, strlen($aConfig['URL']) + 5);
			
			if(strpos($sReferer, $aConfig['URL']) !== false)
			{
				$bAutoLogin = true;
				$sUser = $aConfig['USER'];
				break;
			}
		}
		
	
		if($bAutoLogin === true)
		{
			if($sUser != "")
			{
				
				$User = get_userdatabylogin($sUser);
				$iUserID = $User->ID;
		  
				
				wp_set_current_user($iUserID, $sUser);
				wp_set_auth_cookie($iUserID);
				do_action('wp_login', $sUser);
			}
		}
    }
}

This does an auto login to a user role, not a specific user, but the friend url portion is interesting. I installed this plugin on a test site, but it didnt work (this is two years old). This still relies on a referrer, but curious if the user role part could be changed to a specific user based on the specific friend url… anyway- I think all the pieces are before me at this point… This is my first time with php/plug ins… wish me luck! lol… and thanks again for all of the replies.