Add Login with Yahoo to your php website

By | July 12, 2012

Yahoo OpenId

In the previous article, we learned how to integrate google login using openid in php. Its good to know that Yahoo is also an openid provider and "Login with Yahoo" can be added to a website in a very similar manner. Yahoo's openid discovery url is http://me.yahoo.com/. The rest of the code is very simple.

Code

<?php
/*
The following code example demonstrates, how to implement "Login with Yahoo"
feature in a website. It uses OpenId.

lightopenid library

download :
http://gitorious.org/lightopenid
*/

require '/var/www/lib/lightopenid/openid.php';

try 
{
	# Change 'localhost' to your domain name.
	$openid = new LightOpenID($_SERVER['HTTP_HOST']);
	
	//Not already logged in
	if(!$openid->mode)
	{
		
		//do the login
		if(isset($_GET['login'])) 
		{
            //The google openid url
			$openid->identity = 'https://me.yahoo.com';
			
			//Get additional google account information about the user , name , email , country
			$openid->required = array('contact/email' , 'namePerson/first' , 'namePerson/last' , 'pref/language' , 'contact/country/home'); 
			
			//start discovery
			header('Location: ' . $openid->authUrl());
        }
		else
		{
			//print the login form
			login_form();
		}
		
    }
	
	else if($openid->mode == 'cancel')
	{
		echo 'User has canceled authentication!';
		//redirect back to login page ??
	}
	
	//Echo login information by default
	else
	{
		if($openid->validate())
		{
			//User logged in
			$d = $openid->getAttributes();
			
			$first_name = $d['namePerson/first'];
			$last_name = $d['namePerson/last'];
			$email = $d['contact/email'];
			$language_code = $d['pref/language'];
			$country_code = $d['contact/country/home'];
			
			$data = array(
				'first_name' => $first_name ,
				'last_name' => $last_name ,
				'email' => $email ,
			);
			
            //now signup/login the user.
			print_r($data);
		}
		else
		{
			//user is not logged in
		}
	}
}

catch(ErrorException $e) 
{
	echo $e->getMessage();
}

/*
	This function will print the login form with the button
*/
function login_form()
{
?>
<a href="?login">Login with Yahoo</a>
<?php
}

The above page will present the user with a link for "Login with Yahoo". When the user clicks the link, he/she is taken to yahoo.com where they have to login(if they are not already), and then allow/deny Yahoo to provide information to the requesting website. If they allow, then their details are provided to the requesting website in GET parameters.

Register and Login

Once the details of the user are available, next thing is to register him on the system and log him in. If the email address is already registered, then just proceed with the login without any registration.

About Silver Moon

A Tech Enthusiast, Blogger, Linux Fan and a Software Developer. Writes about Computer hardware, Linux and Open Source software and coding in Python, Php and Javascript. He can be reached at [email protected].

5 Comments

Add Login with Yahoo to your php website
  1. test

    i am not able to download library from url that you provided “http://gitorious.org/lightopenid” its showing 404 no projects found please help me

  2. elham

    Why the firstname and lastname is empty? it return only the email. not the firstname and lastname or fullname .

Leave a Reply

Your email address will not be published. Required fields are marked *