Nowadays every website authenticating a user using some of the greatest trusty third party websites like Google, yahoo or some of the social networks. Today we are learning how to authenticate a user using his Yahoo account. By using third party login we make user feel happy by avoiding filling a registration form. So just start with a simple illustration.
Download Script Live Demo
About Author
The Basic work flow
Little Description
1. If user want to login with his yahoo account he is redirected to yahoo login page from our website for authentication.
2. After successful login yahoo issues user data along with his GUID (Globally Unique Identifier) to our website.
3. We are identifying a user using his GUID in our database. In third step we need to check our database for existence of GUID if it is present the user is old user and display his account. If GUID is not present insert a new record and allot new privileges for him by creating a new account.
4. Display user account with his data.
In order to start working with Yahoo SDK you need to register a web application and get the Application ID, Consumer keys.
1. Creating a new Application
a) Register a new application
b) Get the Application ID, Consumer Key and Consumer Secret.
After getting the Keys for your application next step is to Create a Table for the users.
2. Designing the Database
Create a table for the users with the fields uid, oauth_vendor, oauth_id, name
CREATE TABLE users(
uid INT(11) PRIMARY KEY AUTO_INCREMENT,
oauth_vendor VARCHAR(15),
oauth_id varchar(50),
name VARCHAR(30)
);
uid INT(11) PRIMARY KEY AUTO_INCREMENT,
oauth_vendor VARCHAR(15),
oauth_id varchar(50),
name VARCHAR(30)
);
3. User authentication and Storing User GUID
yahoo_connect.php
<?php
// Include the YOS library.
require 'lib/Yahoo.inc';
include 'db_config.php';
session_start();
define('OAUTH_CONSUMER_KEY', 'your_consumer_key'); // Place Yoru Consumer Key here
define('OAUTH_CONSUMER_SECRET', 'your_consumer_secret'); // Place your Consumer Secret
define('OAUTH_APP_ID', 'your_app_id'); // Place Your App ID here
// If user clicks on LOGIN button
if (array_key_exists("login", $_GET))
{
$session = YahooSession::requireSession(OAUTH_CONSUMER_KEY, OAUTH_CONSUMER_SECRET, OAUTH_APP_ID);
if (is_object($session))
{
$user = $session->getSessionedUser();
$profile = $user->getProfile();
$name = $profile->nickname; // Getting user name
$guid = $profile->guid; // Getting Yahoo ID
//Retriving the user
$query = mysql_query("SELECT guid,name from yahoo_users where guid = '$guid' and oauth_type = 'yahoo'") or die (mysql_error());
$result = mysql_fetch_array($query);
if (empty($result))
{
// user not present in Database. Store a new user and Create new account for him
$query = mysql_query("INSERT INTO yahoo_users(oauth_type, guid, name) VALUES('yahoo', '$guid', '$name')");
$query = mysql_query("SELECT guid,name from yahoo_users where guid = '$guid' and oauth_type = 'yahoo'");
$result = mysql_fetch_array($query);
}
// Creating session variable for User
$_SESSION['login'] = true;
$_SESSION['name'] = $result['name'];
$_SESSION['guid'] = $result['guid'];
$_SESSION['oauth_provider'] = 'yahoo';
}
}
// If user clicks on LOGOUT button
if (array_key_exists("logout", $_GET)) {
// User logging out and Clearing all Session data
YahooSession::clearSession();
unset ($_SESSION['login']);
unset($_SESSION['name']);
unset($_SESSION['guid']);
unset($_SESSION['oauth_provider']); // After logout Redirection here
header("Location: index.php");
}
?>
// Include the YOS library.
require 'lib/Yahoo.inc';
include 'db_config.php';
session_start();
define('OAUTH_CONSUMER_KEY', 'your_consumer_key'); // Place Yoru Consumer Key here
define('OAUTH_CONSUMER_SECRET', 'your_consumer_secret'); // Place your Consumer Secret
define('OAUTH_APP_ID', 'your_app_id'); // Place Your App ID here
// If user clicks on LOGIN button
if (array_key_exists("login", $_GET))
{
$session = YahooSession::requireSession(OAUTH_CONSUMER_KEY, OAUTH_CONSUMER_SECRET, OAUTH_APP_ID);
if (is_object($session))
{
$user = $session->getSessionedUser();
$profile = $user->getProfile();
$name = $profile->nickname; // Getting user name
$guid = $profile->guid; // Getting Yahoo ID
//Retriving the user
$query = mysql_query("SELECT guid,name from yahoo_users where guid = '$guid' and oauth_type = 'yahoo'") or die (mysql_error());
$result = mysql_fetch_array($query);
if (empty($result))
{
// user not present in Database. Store a new user and Create new account for him
$query = mysql_query("INSERT INTO yahoo_users(oauth_type, guid, name) VALUES('yahoo', '$guid', '$name')");
$query = mysql_query("SELECT guid,name from yahoo_users where guid = '$guid' and oauth_type = 'yahoo'");
$result = mysql_fetch_array($query);
}
// Creating session variable for User
$_SESSION['login'] = true;
$_SESSION['name'] = $result['name'];
$_SESSION['guid'] = $result['guid'];
$_SESSION['oauth_provider'] = 'yahoo';
}
}
// If user clicks on LOGOUT button
if (array_key_exists("logout", $_GET)) {
// User logging out and Clearing all Session data
YahooSession::clearSession();
unset ($_SESSION['login']);
unset($_SESSION['name']);
unset($_SESSION['guid']);
unset($_SESSION['oauth_provider']); // After logout Redirection here
header("Location: index.php");
}
?>
Login Page
login.php
<?php
include 'yahoo_connect.php';
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html>
<head>
<title>Yahoo Authentication</title>
</head>
<body>
<?php
if ($_SESSION['login'] == true)
{
echo '<br/><a href="?logout"><img src="images/logout_btn.png" alt="Yahoo Logout"/></a>';
}
else
{
echo '<a href="?login"><img src="images/login_btn.png" alt="Yahoo Login"/></a>';
}
?>
</body>
</html>
include 'yahoo_connect.php';
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html>
<head>
<title>Yahoo Authentication</title>
</head>
<body>
<?php
if ($_SESSION['login'] == true)
{
echo '<br/><a href="?logout"><img src="images/logout_btn.png" alt="Yahoo Logout"/></a>';
}
else
{
echo '<a href="?login"><img src="images/login_btn.png" alt="Yahoo Login"/></a>';
}
?>
</body>
</html>
nice tutorial, always helpful!
ReplyDeleteGood one
ReplyDeleteGreat tutorial, thanks for the informative video and images... very helpful ! 5 stars
ReplyDeleteThanks for sharing that work flow chart.. Nice..
ReplyDelete@dskanth : Thank You for your 5 STARS :)
ReplyDeleteNO NO NO NO NO NO
ReplyDelete---------------
i am dreaming
u r amizing
thank u * 9999999999999
u are help me
^^
u still my bro
god help u
Nice tuts, I hope you can make a google auth
ReplyDeletethanks for sharring Ravi (very helpful), what if the google / gmail oauth login connect :) we wait for the next tutorial, friend :)
ReplyDeleteGoogle oAuth will publish soon.
ReplyDeleteamazing amazing bos...good good
ReplyDeleteinteresting article on oauth login connect
ReplyDeletethanks.. i couldnt understand after step 3 cuz im an asp.net programer. anyway i got the idea
ReplyDeleteyahoo openid is not working why?.if i click loginlink it is displayed like this not redirect to yahoo api page http://xxxxxx.com?login
ReplyDeletewhen i click login button there is no action happened.there is no redirection to yahoo api login page.i gave secret,consumer and api id.but i don't know why it is not working.will it run on localhost. i think that i don'
ReplyDeletet know about how to run this application.please reply me
url:http://www.9lessons.info/2011/01/yahoo-oauth-login-connect.html
same here. nothing happens when we click login. it doesnt redirects to yahoo
ReplyDeleteIs it possible to retrieve the email from this API?
ReplyDeletecan we get the unread mails from yahoo. if yes can you share code for getting mails
ReplyDeleteis there any way to get the yahoo unread mails, if yes please share the code
ReplyDeletehello can some one help ..its not redirecting ..
ReplyDeleteI am facing the problem of oauth_problem=consumer_key_rejected
ReplyDeleteany reason?
Fix error 'oauth_problem=consumer_key_rejected'
ReplyDeletehttp://till.klampaeckel.de/blog/archives/153-Yahoo-oauth_problemconsumer_key_rejected.html
hi i am unable to sue this script can you guide me up usually i have setup the whole script as per guidance still there is no effect when i click on login button pleas tdo help
ReplyDeleteNothing happen.. the last url only show ?login
ReplyDeletewhat happend ? please help me.. it supposed to direct me to yahoo right ?
hi iam getting error
ReplyDeleteFatal error: Call to undefined function curl_init() in C:\xampp\htdocs\yahoo\lib\Yahoo.inc on line 1810
like this .....
can any help me
Fatal error: Call to undefined function curl_init() in C:\xampp\htdocs\yahoo\lib\Yahoo.inc on line 1810
ReplyDeletehiany one help me i am getting a problem when clicking on yahoologin button
Awesome info, thanks!
ReplyDeleteThe question of no redirect after clicking on LOGIN is asked so many times here and still no answer. What do we do please?
ReplyDeletesorry mate, doesnt work...
ReplyDelete$session = YahooSession::requireSession(OAUTH_CONSUMER_KEY, OAUTH_CONSUMER_SECRET, OAUTH_APP_ID);
doesnt return nything
hi i am getting a problem in login with yahoo..
ReplyDeletei am using code--
$session = YahooSession::requireSession(OAUTH_CONSUMER_KEY, OAUTH_CONSUMER_SECRET, OAUTH_APP_ID);
if (is_object($session)) {
$user = $session->getSessionedUser();
$profile = $user->getProfile();
$name = $profile->nickname; // Getting user name
$guid = $profile->guid; // Getting Yahoo ID
}
but when i click on yahoo login button page remain same. i also define OAUTH_CONSUMER_KEY, OAUTH_CONSUMER_SECRET, OAUTH_APP_ID.
but if i do:--
if (!is_object($session)) {echo "djf"; }
its working ..plz help me
hi i am getting a problem in login with yahoo..
ReplyDeletei am using code--
$session = YahooSession::requireSession(OAUTH_CONSUMER_KEY, OAUTH_CONSUMER_SECRET, OAUTH_APP_ID);
if (is_object($session)) {
$user = $session->getSessionedUser();
$profile = $user->getProfile();
$name = $profile->nickname; // Getting user name
$guid = $profile->guid; // Getting Yahoo ID
}
but when i click on yahoo login button page remain same. i also define OAUTH_CONSUMER_KEY, OAUTH_CONSUMER_SECRET, OAUTH_APP_ID.
but if i do:--
if (!is_object($session)) {echo "djf"; }
its working ..plz help me
The question of no redirect after clicking on LOGIN is asked so many times here and still no answer. What do we do please?
ReplyDeletehi, it's isn't working, it's said that
ReplyDeleteStrict Standards: Non-static method YahooSession::requireSession() should not be called statically in ../yahoo_connect.php on line 14
can you tell me, is it ok to change the method to static method ? or maybe you had another solutions, please, response me asap
In Api after getting the Keys for your application check for SUB DIRECTORY option in the list provided below to your keys.
ReplyDeleteSelect the RADIO BUTTON Read/Write Public and Private inorder to get login page..
You won't be redirected to login page and will be in same page until you select this radui button and save the information.
After getting API Keys,
ReplyDeleteYou need to CHeck for SOCIAL DIRECTORY option below keys.
You need to check " Read/Write Public and Private" RADIO BUTTON.
You remain in the same page and won't be redirected to login page until u select this radio button and save the information.
It does not work in my site!
ReplyDeletecan we do the above complete implementation in javascript
ReplyDeleteNishanth: wer to create table for database?
ReplyDelete$session = YahooSession::requireSession(OAUTH_CONSUMER_KEY, OAUTH_CONSUMER_SECRET, OAUTH_APP_ID);
ReplyDeleteis not working..
nice but i don't have lib/Yahoo.inc
ReplyDeleteGood Job
ReplyDeleteCan You please share me how to get group and friendlist for linkedin in php
$session = YahooSession::requireSession(OAUTH_CONSUMER_KEY, OAUTH_CONSUMER_SECRET, OAUTH_APP_ID);
ReplyDeleteis not working..
plzzz help
I hope you can make a tuts about get yahoo contact list. Thanks!
ReplyDeletehello i got following warning, it working but when another user gets logged in than it goest provide name and guid.... what should i do any clue??
ReplyDeleteWarning: Unknown: Your script possibly relies on a session side-effect which existed until PHP 4.2.3. Please be advised that the session extension does not consider global variables as a source of data, unless register_globals is enabled. You can disable this functionality and this warning by setting session.bug_compat_42 or session.bug_compat_warn to off, respectively in Unknown on line 0
it works successfully but sessions are not destroyed properlly.. when new user logged in then it doesn't show name and guid
ReplyDeleteWarning: Unknown: Your script possibly relies on a session side-effect which existed until PHP 4.2.3. Please be advised that the session extension does not consider global variables as a source of data, unless register_globals is enabled. You can disable this functionality and this warning by setting session.bug_compat_42 or session.bug_compat_warn to off, respectively in Unknown on line 0
Why it should need a live server (domain). can't we do it in a localhost or subdomain
ReplyDeleteI am looking to integration the login apps of yahoo, gmail, facebook, twitter and msnlive. Can u help me to put all of them together
ReplyDeleteI downloaded the package and changed the keys and db settings. when i run the script it is not happening. Initially it is not logging in. why?
ReplyDeleteI found the solution why it is not working. In the file 'lib/yahoo.inc' change the callback url to the verified domain url. then it will works. change the callbackurl in the function 'createAuthorizationUrl'
ReplyDeleteOAUTH_APP_ID where i get this?
ReplyDeleteHi, no action when I click Login Button, can you help me ?
ReplyDeletevery nice but how to get user emai
ReplyDeleteHi!
ReplyDeleteIt seems that if you arrive on yahoo registrayion / login page and click "Back" button, the next attempt to login with yahoo throws this error:
Warning: First parameter must either be an object or the name of an existing class in /home/hotpens/www/yahoo/Yahoo.inc on line 1372
Warning: First parameter must either be an object or the name of an existing class in /home/domain/www/yahoo/Yahoo.inc on line 1372
Warning: Cannot modify header information - headers already sent by (output started at /home/domain/www/yahoo/Yahoo.inc:1372) in /home/domain/www/yahoo/Yahoo.inc on line 343
Any idea on how can it be fixed? Also, it throws the same error if you use a linkedin script and not get logged in and after the yahoo one.
Thanks
Awesome login scripts!
get email? help...
ReplyDeleteGreat tutorial!
ReplyDeletePlease give example for the same in java/javascript
ReplyDeleteyou showing something else here and script not working what the hell is this ...
ReplyDeleteHi, the demo is awesome.. But I am not able download the script. I subscribed and confirmation is also done, but during download it says email does not exist..
ReplyDeleteHey Sri,
ReplyDelete$session = YahooSession::requireSession(OAUTH_CONSUMER_KEY, OAUTH_CONSUMER_SECRET, OAUTH_APP_ID);
//""" I could not get any $session Value here so it stops here... plz suggest anything """
if (is_object($session)) {
$user = $session->getSessionedUser();
$profile = $user->getProfile();
$name = $profile->nickname; // Getting user name
$guid = $profile->guid; // Getting Yahoo ID
//Retriving the user
/* $query = mysql_query("SELECT guid,name from yahoo_users where guid = '$guid' and oauth_type = 'yahoo'") or die (mysql_error());
$result = mysql_fetch_array($query);
if (empty($result)) {
// user not present in Database. Store a new user and Create new account for him
$query = mysql_query("INSERT INTO yahoo_users(oauth_type, guid, name) VALUES('yahoo', '$guid', '$name')") or die (mysql_error());
$query = mysql_query("SELECT guid,name from yahoo_users where guid = '$guid' and oauth_type = 'yahoo'") or die (mysql_error());
$result = mysql_fetch_array($query);
}*/
// Creating session variable for User
$_SESSION['login'] = true;
$_SESSION['name'] = $result['name'];
$_SESSION['guid'] = $result['guid'];
$_SESSION['oauth_provider'] = 'yahoo';
}
else
{
echo($session);
echo("you--------- are here");
die;
}
}
$session = YahooSession::requireSession(OAUTH_CONSUMER_KEY, OAUTH_CONSUMER_SECRET, OAUTH_APP_ID);
ReplyDeleteit is returning null ..Please help
Hi All,
ReplyDeleteAfter configured the download script. I have added some require information in yahoo_connect.php( for key) and db_config.php( fro database) file but when we run the script we are getting below error.
Strict Standards: Non-static method YahooSession::requireSession() should not be called statically in /opt/lampp/htdocs/zorbid/zorbid/Yahoo_Oauth_YOS/yahoo_connect.php on line 17
Strict Standards: Non-static method YahooSession::initSession() should not be called statically in /opt/lampp/htdocs/zorbid/zorbid/Yahoo_Oauth_YOS/lib/Yahoo.inc on line 437
Strict Standards: Non-static method YahooSession::checkSession() should not be called statically in /opt/lampp/htdocs/zorbid/zorbid/Yahoo_Oauth_YOS/lib/Yahoo.inc on line 555
Strict Standards: Non-static method YahooSession::redirectForAuthorization() should not be called statically in /opt/lampp/htdocs/zorbid/zorbid/Yahoo_Oauth_YOS/lib/Yahoo.inc on line 561
Strict Standards: Non-static method YahooSession::createAuthorizationUrl() should not be called statically in /opt/lampp/htdocs/zorbid/zorbid/Yahoo_Oauth_YOS/lib/Yahoo.inc on line 340
Strict Standards: Non-static method YahooUtil::current_url() should not be called statically in /opt/lampp/htdocs/zorbid/zorbid/Yahoo_Oauth_YOS/lib/Yahoo.inc on line 474
Strict Standards: Non-static method YahooAuthorization::getRequestToken() should not be called statically in /opt/lampp/htdocs/zorbid/zorbid/Yahoo_Oauth_YOS/lib/Yahoo.inc on line 478
Strict Standards: Non-static method YahooLogger::debug() should not be called statically, assuming $this from incompatible context in /opt/lampp/htdocs/zorbid/zorbid/Yahoo_Oauth_YOS/lib/Yahoo.inc on line 1866
Strict Standards: Non-static method YahooLogger::error() should not be called statically in /opt/lampp/htdocs/zorbid/zorbid/Yahoo_Oauth_YOS/lib/Yahoo.inc on line 1322
Strict Standards: Non-static method YahooLogger::error() should not be called statically in /opt/lampp/htdocs/zorbid/zorbid/Yahoo_Oauth_YOS/lib/Yahoo.inc on line 489
Strict Standards: Non-static method YahooLogger::error() should not be called statically in /opt/lampp/htdocs/zorbid/zorbid/Yahoo_Oauth_YOS/lib/Yahoo.inc on line 348
Notice: Undefined index: login in /opt/lampp/htdocs/zorbid/zorbid/Yahoo_Oauth_YOS/index.php on line 16
$session = YahooSession::requireSession(CONSUMER_KEY,CONSUMER_SECRET,APPID);
ReplyDeletereturn Null
Please check it and tell it for me
YahooSession::requireSession
ReplyDeleteit is returning null ..
Click login button , not redirect
Please help me !
great job.Can you Please Update to get yahoo! contacts (Like invite your friends in facebook).
ReplyDeleteI'm getting error: "Fatal error: Call to undefined function curl_init() in F:\Software\wamp\www\test\Yahoo_Oauth_YOS\Yahoo_Oauth_YOS\lib\Yahoo.inc "
ReplyDeleteHai! I have some problem with your script:
ReplyDelete1. when test it in localhost it no return nothing when i click button login i see my URL add (..my root url..)?login with errors undefined index in Yahoo_Oauth_YOS\index.php on line 17.
2.I am try test it on server but after i pushing this script to my server it's make all my code is not run. my index page nothing return something on browser .
3. if this script is run well how i can get contact list more .
please help me to show my problem. by the way i note all command you not reply i think it's may be your site no function reply command so you can help me some idea by mail : [email protected]
HI... how to get YM chat log using this project.
ReplyDeletethanks before :)
i mean i want to save YM chat into database(myql) :)
ReplyDeleteThis does not provide user's email address. Did you try to get the login email address?
ReplyDelete$session = YahooSession::requireSession(OAUTH_CONSUMER_KEY, OAUTH_CONSUMER_SECRET, OAUTH_APP_ID);
ReplyDeleteits not working plz help me........................
would love a tutorial on getting contact list from yahoo accounts!
ReplyDeletethanks for you script it is very helpful
ReplyDeleteStrict Standards: Non-static method YahooSession::requireSession() should not be called statically in ... How can i fix this bug
ReplyDeletethnks.. want some code to retrieve contacts from yahoo to send invitation to my website.. can any1 help me???
ReplyDeleteStrict Standards: Non-static method YahooSession::requireSession() should not be called statically in ... How can i fix this bug
ReplyDeleteStrict Standards: Non-static method YahooSession::redirectForAuthorization() should not be called statically in D:\xampp\htdocs\loginyahoo\lib\Yahoo.inc on line 562
ReplyDeleteNotice: Undefined index: login in D:\xampp\htdocs\loginyahoo\index.php on line 16
I'm facing this problem please give some solution
thank you nice script but $session = YahooSession::requireSession(OAUTH_CONSUMER_KEY, OAUTH_CONSUMER_SECRET, OAUTH_APP_ID); return null please any help me why problem ......
ReplyDeleteI have the same problem $session = YahooSession::requireSession(OAUTH_CONSUMER_KEY, OAUTH_CONSUMER_SECRET, OAUTH_APP_ID); returns null.
ReplyDeleteHow can i fix this?
about problem $session = YahooSession::requireSession(OAUTH_CONSUMER_KEY, OAUTH_CONSUMER_SECRET, OAUTH_APP_ID); returns null:
ReplyDeletei tick to Social Directory/Read Public - With your user's permission, you can read their Profile information.... on my yahoo API then it work. Hope that help you :)
just put isset($requestToken->sessionHandle) instead of property_exists($requestToken, "sessionHandle") for the issue :
ReplyDeleteWarning: First parameter must either be an object or the name of an existing class in /home/hotpens/www/yahoo/Yahoo.inc on line 1372
Warning: First parameter must either be an object or the name of an existing class in /home/domain/www/yahoo/Yahoo.inc on line 1372
Warning: Cannot modify header information - headers already sent by (output started at /home/domain/www/yahoo/Yahoo.inc:1372) in /home/domain/www/yahoo/Yahoo.inc on line 343
I have done it ...
It is really nice.
ReplyDeleteThanks for great job.
But I am unable to get user email address.
Can anyone please help me?
Login : http://developer.apps.yahoo.com/projects/
ReplyDeleteedit your projects
Check Permissions for that projects
Login and Edit your project on Yahoo API.
ReplyDeleteCheck Permission in bottom
Does this still work? I am getting 401 "Custom port is not allowed or the host is not registered with this consumer key." error with sample code... i have checked my consumer keys and other stuff several times...
ReplyDeletei seriously wanna cry right now!! at yahoo developer screen i entered my website with www. prefix and on my browser i was writing my website without it www. prefix and that was why it didnt work!! This is ridiculous, they should have allowed it..
ReplyDeleteReally awesome.
ReplyDeleteredirecting to same page when i click login and getting this error Notice: Undefined index: login in /home2/
ReplyDeletehii i'm not getting the Name and Email Address after login into..
ReplyDeleteSince i am a beginner i dont know how to Link or Connect the above given sdk folder to my login page . so i kindly request you peoples to please guide me....
ReplyDeletethank you nice script but $session = YahooSession::requireSession(OAUTH_CONSUMER_KEY, OAUTH_CONSUMER_SECRET, OAUTH_APP_ID); return null please any help me why problem ......
ReplyDeleteThis code not working perfectly...plz somebody help..
ReplyDeleteWhen i click the login button it redirects nothing!...plz give proper datatbase tables details....
ReplyDeleteHi,
ReplyDeleteI couldn't able to see the images ... can anyone post it again! Thanks in advance.
how can i get that yahoo library?
ReplyDelete