Advertisement
nigerianprincemoney

fbconnect.php

Jan 13th, 2013
19
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.28 KB | None | 0 0
  1. <?php
  2. if(!isset($_SESSION['user']))
  3. {
  4.     //Application Configurations
  5.     $app_id = "3xxxxxxx492";
  6.     $app_secret = "0edd9byyyyyyyyyy62bb7";
  7.     $site_url   = "zzzzz.com";
  8.  
  9.     try{
  10.         include_once "src/facebook.php";
  11.     }catch(Exception $e){
  12.         error_log($e);
  13.     }
  14.     // Create our application instance
  15.     $facebook = new Facebook(array(
  16.         'appId'     => $app_id,
  17.         'secret'    => $app_secret,
  18.         ));
  19.  
  20.     // Get User ID
  21.     $fbid = $facebook->getUser();
  22.     // We may or may not have this data based
  23.     // on whether the user is logged in.
  24.     // If we have a $fbid id here, it means we know
  25.     // the user is logged into
  26.     // Facebook, but we don’t know if the access token is valid. An access
  27.     // token is invalid if the user logged out of Facebook.
  28.     //print_r($fbid);
  29.     if($fbid){
  30.         // Get logout URL
  31.         $logoutUrl = $facebook->getLogoutUrl();
  32.     }else{
  33.         // Get login URL
  34.         $loginUrl = $facebook->getLoginUrl(array(
  35.             'scope'         => 'read_stream, email, user_about_me',
  36.             'redirect_uri'  => $site_url,
  37.             ));
  38.     }
  39.  
  40.     if($fbid){
  41.  
  42.         try{
  43.         // Proceed knowing you have a logged in user who's authenticated.
  44.         $fbid_profile = $facebook->api('/me');
  45.         //Connecting to the database. You would need to make the required changes in the common.php file
  46.         //In the common.php file you would need to add your Hostname, username, password and database name!
  47.         mysqlc();
  48.        
  49.         $fullname = GetSQLValueString($fbid_profile['name'], "text");
  50.         $email = GetSQLValueString($fbid_profile['email'], "text");
  51.         $gender = GetSQLValueString($fbid_profile['gender'], "text");
  52.         $bio = GetSQLValueString($fbid_profile['bio'], "text");
  53.         $query = sprintf("SELECT * FROM newmember WHERE email = %s",$email);
  54.         $res = mysql_query($query) or die('Query failed: ' . mysql_error() . "<br />\n$sql");
  55.         if(mysql_num_rows($res) == 0)
  56.         {
  57.             $iquery = sprintf("INSERT INTO newmember values('',%s,%s,%s,%s,'yes')",$fullname,$email,$gender,$bio);
  58.             $ires = mysql_query($iquery) or die('Query failed: ' . mysql_error() . "<br />\n$sql");
  59.             $_SESSION['user'] = $fbid_profile['email'];
  60.             $_SESSION['id'] = $fbid_profile['id'];
  61.         }
  62.         else
  63.         {
  64.             $row = mysql_fetch_array($res);
  65.             $_SESSION['user'] = $row['email'];
  66.             $_SESSION['id'] = $fbid_profile['id'];
  67.         }
  68.         }catch(FacebookApiException $e){
  69.                 error_log($e);
  70.                 $fbid = NULL;
  71.             }
  72.        
  73.     }
  74. }
  75. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement