0

这里的问题是......只有注册用户才能连接,如果有新用户,它将无法将插入数据获取到数据库。请帮忙。谢谢。

$app_id = "";
$app_secret = ""
$facebook = new Facebook(array(
    'appId' => $app_id,
    'secret' => $app_secret,
    'cookie' => true
));
mysql_connect('localhost', '#', '#');
mysql_select_db('#');
# Let's see if we have an active session
$session = $facebook->getSession();

if(!empty($session)) {
    # Active session, let's try getting the user id (getUser()) and user info (api->   ('/me'))
    try{
        $uid = $facebook->getUser();
        $user = $facebook->api('/me');   
    } catch (Exception $e){}
        # We have an active session, let's check if we have already registered the user
        $query = mysql_query("SELECT * FROM usertable WHERE fbid = ". $user['id']);
    $result = mysql_fetch_array($query);     
    if($result['fbid'] = $_SESSION['id']) {      
        // this sets variables in the session 
        //$_SESSION['id'] = $result['fbid'];
        // $_SESSION['fullname'] = $result['fullname'];
        //$_SESSION['email'] = $result['email'];

        $login_url = $facebook->getLoginUrl();
        header("Location: ".$login_url);
    }
}else if(!empty($user)){     
    # If not, let's add it to the database
    //}
    # We have an active session, let's check if we have already registered the user
    $query = mysql_query("SELECT * FROM usertable WHERE fbid = ". $user['id']);
    $result2 = mysql_fetch_array($query);
    if(empty($result)){
        $query = mysql_query("INSERT INTO usertable (fbid, username, email) VALUES   ({$user['id']}, '{$user['username']}'");
        $query = mysql_query("SELECT * FROM users WHERE id = " . mysql_insert_id());
        $result2 = mysql_fetch_array($query);

        // this sets variables in the session 
        $_SESSION['id'] = $result2['id'];
        $_SESSION['fullname'] = $result2['fullname'];
        $_SESSION['email'] = $result2['email'];
        //$_SESSION['username'] = $result['username'];
    } else {
        # For testing purposes, if there was an error, let's kill the script
        die("There was an error.");
    }
} else {
    # There's no active session, let's generate one
    $login_url = $facebook->getLoginUrl();
    header("Location: ".$login_url);
}
4

0 回答 0