开发者

storing facebook session from javascript to php

开发者 https://www.devze.com 2023-03-01 09:09 出处:网络
Is there a way to store the session got from javascript to PHP\'s session? I know that I have to do a POST, can someone give me an example? This is what I have so far: I want to store the uid in PHP\'

Is there a way to store the session got from javascript to PHP's session? I know that I have to do a POST, can someone give me an example? This is what I have so far: I want to store the uid in PHP's session

<script type="text/javascript开发者_StackOverflow">
                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>


apparently you know what you want to do, so using Jquery's ajax methods. There you can enable the post method.

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");
    }

});


The uid is stored in the FB object as response.session.uid, which is the parameter you need to send via 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>

The session data is all stored on a cookie that you read in javaScript. You can of course access this data directly from PHP if you want using something like this code:

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'] has the required data!

There is also a PHP SDK which creates an object containing this data: https://github.com/facebook/php-sdk/

You can get UID using the getUser() method.....

0

精彩评论

暂无评论...
验证码 换一张
取 消