0

使用以下代码,我可以通过 Facebook 执行登录过程,但永远不会完成(即我的应用程序永远不会收到令牌)。分不清断线。
网址(www.manlymoose.com)。

配置文件

<?php
session_start();
$appID='*******';
$appSecret='*******';
if($_SERVER['HTTP_HOST']=='localhost'){
    $base_url='http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
}else{
    $base_url='http://'.$_SERVER['HTTP_HOST'];  
}
?>

索引.php

<?php 
session_start();    
if(!isset($_SESSION['User']) && empty($_SESSION['User'])){
    $fbimg = '<img src="images/facebook.png" id="facebook" style="cursor:pointer;float:left;" />';
}else{
$fbimg = '<img src="https://graph.facebook.com/'. $_SESSION['User']['id'] .'/picture" width="30" height="30"/><div>'.$_SESSION['User']['name'].'</div><a href="'.$_SESSION['logout'].'">Logout</a>';
}
?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<link href="scripts/style.css" type="text/css" rel="stylesheet"/>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.3/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript" src="js/oauthpopup.js"></script>
<script type="text/javascript">
$(document).ready(function(){
    $('#facebook').click(function(e){
        $.oauthpopup({
            path: 'login.php',
            width:600,
            height:300,
            callback: function(){
                window.location.reload();
            }
        });
        e.preventDefault();
    });
});
</script>
</head>
<body>
    <?php echo $fbimg; ?>
</body>
</html>

登录.php

<?php
require 'config.php';
require 'lib/facebook/facebook.php';

$facebook = new Facebook(array(
        'appId'     => $appID,
        'secret'    => $appSecret,
        ));     
$user = $facebook->getUser();
if($user){

    try{
        $user_profile = $facebook->api('/me');
        $params = array('next' => 'http://www.manlymoose.com/logout.php');
        //logout url
        $logout = $facebook->getLogoutUrl($params);
        $_SESSION['User']=$user_profile;
        $_SESSION['logout']=$logout;
    }catch(FacebookApiException $e){
        error_log($e);
        $user = NULL;
    }       
}
if(empty($user)){
//login url 
$loginurl = $facebook->getLoginUrl(array(
        'scope'         => 'email,read_stream, publish_stream, user_birthday, user_location, user_work_history, user_hometown, user_photos',
        'redirect_uri'  => $base_url,
        'display'=>'popup'
        ));
header('Location: '.$loginurl);
}
?>
<!-- after authentication close the popup -->
<script type="text/javascript">
window.close();
</script>

oauthpopup.js

(function (jQuery) {
    jQuery.oauthpopup = function (options) {
        options.windowName = options.windowName || 'ConnectWithOAuth';
        options.windowOptions = options.windowOptions || 'location=0,status=0,width='+options.width+',height='+options.height+',scrollbars=1';
        options.callback = options.callback || function () {
            window.location.reload();
        };
        var that = this;
        that._oauthWindow = window.open(options.path, options.windowName, options.windowOptions);
        that._oauthInterval = window.setInterval(function () {
            if (that._oauthWindow.closed) {
                window.clearInterval(that._oauthInterval);
                options.callback();
            }
        }, 1000);
    };
})(jQuery);
4

0 回答 0