Facebook Integration in a PHP site

2019-04-14 04:42发布

问题:

I am trying to integrate my existing php site with Facebook Login functionality. I currently have a simple login registration username, password and email for activation and their details are stored in a MYSQL database. I want to add additional login access for Facebook users. I am worried about how i will be able to integrate my current login access with the new facebook access. Principal on my mind is that all my pages require users to be logged in and i use cookies to store session variables like nickname which i use to build content on my pages.

I am confused by the many approaches I have researched on the web, some are now sadly obsolete and others seem to be half solutions. These three keep re-occurring.

A) Login.php, Register.php and fbReg.php The login.php and register.php appear when logged in using fb login/register to create a session and pass authentication to the fbReg.php but it just stops there with a blank screen after the page is redirected.

(a snippet from the Register.php)

<script type="text/javascript">
FB.init({appId: '<?php echo $fb_app_id; ?>', status: true,
    cookie: true, xfbml: true});

<?php
    if ($errors == 0) {
    ?>
    $('#form_hidden').hide();
<?php } else { ?>
    checkInput('username');
    checkInput('email');
    checkInput('lastname');
    checkInput('firstname');
    checkInput('password');
    checkInput('password2');
<?php } ?>
</script>

B) Legacy Connect Auth http://developers.facebook.com/docs/authentication/connect_auth/ Using the above link I constructed some test scripts just to see if the authentication would work. Fortunately on my site I have third party ads or webframes but followed the steps to the letter building a interstitial_page and redirected page from my main login page. However I keep getting a null session.

(snippet from the php login page)

$api_key = "YOUR_API_KEY";
$interstitial_page = "YOUR_SECURE_URL"; //URL with no 3rd party apps

$url='http://www.facebook.com/login.php?api_key=' . $api_key  
. '&session_version=3&next=' . urlencode($interstitial_page)
. '&v=1.0&return_session=1&fbconnect=1'
. '&cancel_url=' . urlencode($interstitial_page);

echo "Welcome to the Old Auth flow";
echo "<p>";

echo("<a href='" . $url . "'>"
. "<img src='http://static.ak.facebook.com/images/"
. "devsite/facebook_login.gif'></a>");

C) OAuth 2.0 protocol http://developers.facebook.com/docs/authentication/ when I use this again using the following script:

 https://www.facebook.com/dialog/oauth?
 client_id=YOUR_APP_ID&redirect_uri=YOUR_URL

I have tried and failed to use this to capture User info for my new facebook site users and store it to MySQL database then integrate this authenticated facebook access with the 'normal' web access user session since that has assocaiated session variables which are used the the user experience.

Any help would be appreciated, im struggling at the moment.

Thanks Michael

i get a blank screen on the fbReg.php

回答1:

I just did this very thing for my site last week, so I know how hard it is to sort through all of the many methods, many of which are outdated. This was my approach:

  1. I placed the "Login in with Facebook" link on my site which redirects to my normal registration page.

    <a href="http://www.facebook.com/dialog/oauth?client_id=<? echo $FACEBOOK_APP_ID ?>&redirect_uri=http://www.yoursite.com/register.php&scope=email">Log in with Facebook</a>
    
  2. My registration script looks like this:

    <html xmlns="http://www.w3.org/1999/xhtml" xmlns:fb="http://www.facebook.com/2008/fbml">
    <head></head>
    <body>
    <div id="fb-root"></div>
    <script src="http://connect.facebook.net/en_US/all.js"></script>
    <script>
    FB.init({appId: '<? echo $FACEBOOK_APP_ID ?>', status: true, cookie: true, xfbml: true});
    FB.Event.subscribe('auth.login', function(response) { window.location.reload(); });
    </script>
    
    function get_facebook_cookie($app_id, $app_secret) 
    {
        $args = array();
        parse_str(trim($_COOKIE['fbs_' . $app_id], '\\"'), $args);
    
        ksort($args);
        $payload = '';
        foreach ($args as $key => $value) {
        if ($key != 'sig') {
        $payload .= $key . '=' . $value;
        }
        }
        if (md5($payload . $app_secret) != $args['sig']) {
        return null;
        }
        return $args;
    }
    
    $cookie = get_facebook_cookie($FACEBOOK_APP_ID, $FACEBOOK_SECRET);
    if($cookie)
    { 
        $access_token = $cookie['access_token'];
    
    }
    $url = 'https://graph.facebook.com/me?access_token='.$access_token;
    $graph = file_get_contents($url);
    if($graph) 
    {
    
        $fbuser = json_decode($graph);
        $userid = $fbuser->id;
        $email = mysql_real_escape_string($fbuser->email);
    
        /********************** 
        I placed code here to check if the  email already exists in my database, which means they are already a member. 
        In that case, I retrieve their username and password from the database and automatically log them 
        in by rerouting them to my login script with their crenditials sent as well.
        ***********************/
    
        /********************** 
        If the email is not already in my database, then I know it's a new user and so I prompt them to make a username here. 
        ***********************/
    }
    //The user either opted to log in using my traditional non-Facebook method, or something went wrong with the FB authentication
    else 
    {
    
        /********************** 
        Show my normal registration form that prompts for username, email, etc 
        ***********************/
    }
    
  3. In my login script, I set the username, etc cookies as normal.

Hope this helps!



回答2:

I can't tell if you are talking about the social plug-ins, but if you are, these Facebook Plug-ins have generators built into the pages which will generate the code you need.

If you are not looking for plugins but instead the information for the users, I still recommend that you run the generators to get the code and see how Facebook gets the information for the users.

Following the path of the simple scripts should get you to where you need to be.