2011-04-29 66 views
1

有沒有一種方法來存儲從JavaScript獲得的會話到PHP的會話?我知道我必須做一個POST,有人可以給我一個例子嗎?這是我迄今爲止:我想在PHP中的會話的UID存儲Facebook會話從JavaScript到PHP

<script type="text/javascript"> 
       jQuery(document).ready(function() { 
        FB.init({appId: '135570939849404', status: true, cookie: true, xfbml: true}); 

        FB.getLoginStatus(function(response) { 
        if (response.session) { 
         //do smething? 
        }else { 
         window.location = "index.php" 
        }  
        }); 


       }); 
     </script> 
+0

您是否嘗試過[Facebook的PHP- SDK(http://github.com/facebook/php-sdk)?它會照顧你這個... – ifaour 2011-04-29 15:43:59

回答

0

顯然你知道你想要做什麼,所以使用jQuery的AJAX方法。在那裏你可以啓用post方法。

jQuery.ajax({ 
    async:true, 
    type: "POST", 
    url: ("/yourTarget.php"), 
    dataType : 'json', 

    data: {email : 'Jeremy'}, 

    success : function(data){ 

     alert("success"); 

    }, 

    error : function(XMLHttpRequest, textStatus, errorThrown) { 
     alert("error"); 
    } 

}); 
0

該uid作爲response.session.uid存儲在FB對象中,這是您需要通過Ajax發送的參數。

<script type="text/javascript"> 
      jQuery(document).ready(function() { 
       FB.init({appId: '135570939849404', status: true, cookie: true, xfbml: true}); 

       FB.getLoginStatus(function(response) { 
       if (response.session) { 
        jQuery.ajax({ 
           async:true, 
           type: "POST", 
           url: ("/yourTarget.php"), 
           dataType : 'json', 
           data: {uid : response.session.uid}, 
           success : function(data){ 
            alert("success"); 
           }, 
           error : function(XMLHttpRequest, textStatus, errorThrown) { 
            alert("error"); 
           } 

         }); 

       }else { 
        window.location = "index.php" 
       }  
       }); 


      }); 
    </script> 

會話數據全部存儲在您在javaScript中讀取的cookie中。你當然也可以在這個數據直接從PHP訪問的,如果你想使用這樣的代碼:

function get_facebook_session_data($app_id, $application_secret) { 
    $cookieName = 'fbs_' . $app_id; 
    if (!isset($_COOKIE[$cookieName])) { 
     return null; 
    } 
    $fb_session = array(); 
    //If magic_quotes is set then stripslashes else strip doublequotes then trim the cookie contents. 
    //Parse the cookie - loading each parameter into an associative array $fb_session. 
    parse_str(trim(get_magic_quotes_gpc() ? stripslashes($_COOKIE[$cookieName]): $_COOKIE[$cookieName],'"'), $fb_session); 
    return $fb_session; 

}

$ fb_session [「UID」]有需要的數據!

還有一個PHP SDK它創建包含此數據的對象: https://github.com/facebook/php-sdk/

可以使用的getUser()方法UID得到.....