设为首页收藏本站

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 223|回复: 0

Yahoo Oauth Login Connect

[复制链接]
发表于 2014-3-26 14:02:23 | 显示全部楼层 |阅读模式
本帖最后由 Test 于 2014-3-26 14:05 编辑

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.

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

  1. CREATE TABLE users(
  2. uid INT(11) PRIMARY KEY AUTO_INCREMENT,
  3. oauth_vendor VARCHAR(15),
  4. oauth_id varchar(50),
  5. name VARCHAR(30)
  6. );
复制代码



3. User authentication and Storing User GUID

yahoo_connect.php
  1. <?php

  2. // Include the YOS library.
  3. require 'lib/Yahoo.inc';
  4. include 'db_config.php';

  5. session_start();

  6. define('OAUTH_CONSUMER_KEY', 'your_consumer_key'); // Place Yoru Consumer Key here
  7. define('OAUTH_CONSUMER_SECRET', 'your_consumer_secret'); // Place your Consumer Secret
  8. define('OAUTH_APP_ID', 'your_app_id'); // Place Your App ID here

  9. // If user clicks on LOGIN button
  10. if (array_key_exists("login", $_GET))
  11. {
  12. $session = YahooSession::requireSession(OAUTH_CONSUMER_KEY, OAUTH_CONSUMER_SECRET, OAUTH_APP_ID);
  13. if (is_object($session))
  14. {
  15. $user = $session->getSessionedUser();
  16. $profile = $user->getProfile();
  17. $name = $profile->nickname; // Getting user name
  18. $guid = $profile->guid; // Getting Yahoo ID

  19. //Retriving the user
  20. $query = mysql_query("SELECT guid,name from yahoo_users where guid = '$guid' and oauth_type = 'yahoo'") or die (mysql_error());
  21. $result = mysql_fetch_array($query);

  22. if (empty($result))
  23. {
  24. // user not present in Database. Store a new user and Create new account for him
  25. $query = mysql_query("INSERT INTO yahoo_users(oauth_type, guid, name) VALUES('yahoo', '$guid', '$name')");
  26. $query = mysql_query("SELECT guid,name from yahoo_users where guid = '$guid' and oauth_type = 'yahoo'");
  27. $result = mysql_fetch_array($query);
  28. }

  29. // Creating session variable for User
  30. $_SESSION['login'] = true;
  31. $_SESSION['name'] = $result['name'];
  32. $_SESSION['guid'] = $result['guid'];
  33. $_SESSION['oauth_provider'] = 'yahoo';
  34. }
  35. }

  36. // If user clicks on LOGOUT button
  37. if (array_key_exists("logout", $_GET)) {
  38. // User logging out and Clearing all Session data
  39. YahooSession::clearSession();
  40. unset ($_SESSION['login']);
  41. unset($_SESSION['name']);
  42. unset($_SESSION['guid']);
  43. unset($_SESSION['oauth_provider']); // After logout Redirection here
  44. header("Location: index.php");
  45. }
  46. ?>
复制代码



Login Page

login.php
  1. <?php
  2. include 'yahoo_connect.php';
  3. ?>

  4. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN">
  5. <html>
  6. <head>
  7. <title>Yahoo Authentication</title>
  8. </head>
  9. <body>

  10. <?php
  11. if ($_SESSION['login'] == true)
  12. {
  13. echo '<br/><a href="?logout"><img src="images/logout_btn.png" alt="Yahoo Logout"/></a>';
  14. }
  15. else
  16. {
  17. echo '<a href="?login"><img src="images/login_btn.png" alt="Yahoo Login"/></a>';
  18. }
  19. ?>

  20. </body>
  21. </html>
复制代码




您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|小黑屋|BC Morning Website ( Best Deal Inc. 001 )  

GMT-8, 2025-12-12 20:57 , Processed in 0.016655 second(s), 16 queries .

Supported by Best Deal Online X3.5

© 2001-2025 Discuz! Team.

快速回复 返回顶部 返回列表