2013-04-18 93 views
1

我试图使一个网站与Facebook登录按钮,我尝试使用PHP SDK,我有一个问题:当我登录会话不保存,所以它没有找到它它的行为就像没有设置。PHP Facebook的登录与Mysql

<?php 
if(!isset($_SESSION['user'])) 
{ 
    //Application Configurations 
    $app_id  = "MY_API_KEY_HERE"; 
    $app_secret = "MY_SECRET_KEY_HERE"; 
    $site_url = "http://localhost/"; 

    try{ 
     include_once "src/facebook.php"; 
    }catch(Exception $e){ 
     error_log($e); 
    } 
    // Create our application instance 
    $facebook = new Facebook(array(
     'appId'  => $app_id, 
     'secret' => $app_secret, 
     )); 

    // Get User ID 
    $user = $facebook->getUser(); 
    // We may or may not have this data based 
    // on whether the user is logged in. 
    // If we have a $user id here, it means we know 
    // the user is logged into 
    // Facebook, but we don’t know if the access token is valid. An access 
    // token is invalid if the user logged out of Facebook. 
    //print_r($user); 
    if($user){ 
     // Get logout URL 
     $logoutUrl = $facebook->getLogoutUrl(); 
    }else{ 
     // Get login URL 
     $loginUrl = $facebook->getLoginUrl(array(
      'scope'   => 'read_stream, publish_stream, email, user_about_me', 
      'redirect_uri' => $site_url, 
      )); 
    } 

    if($user){ 

     try{ 
     // Proceed knowing you have a logged in user who's authenticated. 
     $user_profile = $facebook->api('/me'); 
     //Connecting to the database. You would need to make the required changes in the common.php file 
     //In the common.php file you would need to add your Hostname, username, password and database name! 
     mysqlc(); 

     $name = GetSQLValueString($user_profile['name'], "text"); 
     $email = GetSQLValueString($user_profile['email'], "text"); 
     $gender = GetSQLValueString($user_profile['gender'], "text"); 
     $bio = GetSQLValueString($user_profile['bio'], "text"); 
     $query = sprintf("SELECT * FROM newmember WHERE email = %s",$email); 
     $res = mysql_query($query) or die('Query failed: ' . mysql_error() . "<br />\n$sql"); 
     if(mysql_num_rows($res) == 0) 
     { 
      $iquery = sprintf("INSERT INTO newmember values('',%s,%s,%s,%s,'yes')",$name,$email,$gender,$bio); 
      $ires = mysql_query($iquery) or die('Query failed: ' . mysql_error() . "<br />\n$sql"); 
      $_SESSION['user'] = $user_profile['email']; 
      $_SESSION['id'] = $user_profile['id']; 
     } 
     else 
     { 
      $row = mysql_fetch_array($res); 
      $_SESSION['user'] = $row['email']; 
      $_SESSION['id'] = $user_profile['id']; 
     } 
     }catch(FacebookApiException $e){ 
       error_log($e); 
       $user = NULL; 
      } 

    } 
} 
?> 
+1

no session_start()在开始 –

+0

不工作,即使我把它,它不工作 –

回答

2

到Facebook集成和登录,PHP Facebook Login And Integration in PHP,它从这里详细解释和下载Facebook SDK。

<?php 
session_start(); 
// Include the autoloader provided in the SDK 
require_once __DIR__ . '/facebook-php-sdk/autoload.php'; 

// Include required libraries 
use Facebook\Facebook; 
use Facebook\Exceptions\FacebookResponseException; 
use Facebook\Exceptions\FacebookSDKException; 

$appId = 'YOUR APP ID'; //Facebook App ID 
$appSecret = 'YOUR APP SECRET KEY'; //Facebook App Secret 
$redirectURL = 'YOUR CALLBACK URL'; //Callback URL 
$fbPermissions = array('email'); //Optional permissions 

$fb = new Facebook(array(
    'app_id' => $appId, 
    'app_secret' => $appSecret, 
    'default_graph_version' => 'v2.9', 
     )); 

// Get redirect login helper 
$helper = $fb->getRedirectLoginHelper(); 

// Try to get access token 
try { 
    // Already login 
    if (isset($_SESSION['facebook_access_token'])) { 
     $accessToken = $_SESSION['facebook_access_token']; 
    } else { 
     $accessToken = $helper->getAccessToken(); 
    } 

    if (isset($accessToken)) { 
     if (isset($_SESSION['facebook_access_token'])) { 
      $fb->setDefaultAccessToken($_SESSION['facebook_access_token']); 
     } else { 
      // Put short-lived access token in session 
      $_SESSION['facebook_access_token'] = (string) $accessToken; 

      // OAuth 2.0 client handler helps to manage access tokens 
      $oAuth2Client = $fb->getOAuth2Client(); 

      // Exchanges a short-lived access token for a long-lived one 
      $longLivedAccessToken = $oAuth2Client->getLongLivedAccessToken($_SESSION['facebook_access_token']); 
      $_SESSION['facebook_access_token'] = (string) $longLivedAccessToken; 

      // Set default access token to be used in script 
      $fb->setDefaultAccessToken($_SESSION['facebook_access_token']); 
     } 

     // Redirect the user back to the same page if url has "code" parameter in query string 
     if (isset($_GET['code'])) { 

      // Getting user facebook profile info 
      try { 
       $profileRequest = $fb->get('/me?fields=name,first_name,last_name,email,link,gender,locale,picture'); 
       $fbUserProfile = $profileRequest->getGraphNode()->asArray(); 
       // Here you can redirect to your Home Page. 
       echo "<pre/>"; 
       print_r($fbUserProfile); 
      } catch (FacebookResponseException $e) { 
       echo 'Graph returned an error: ' . $e->getMessage(); 
       session_destroy(); 
       // Redirect user back to app login page 
       header("Location: ./"); 
       exit; 
      } catch (FacebookSDKException $e) { 
       echo 'Facebook SDK returned an error: ' . $e->getMessage(); 
       exit; 
      } 
     } 
    } else { 
     // Get login url 

     $loginURL = $helper->getLoginUrl($redirectURL, $fbPermissions); 
     header("Location: " . $loginURL); 

    } 
} catch (FacebookResponseException $e) { 
    echo 'Graph returned an error: ' . $e->getMessage(); 
    exit; 
} catch (FacebookSDKException $e) { 
    echo 'Facebook SDK returned an error: ' . $e->getMessage(); 
    exit; 
} 
?>