在facebook登录后重定向到URL

时间:2011-04-02 04:27:24

标签: php facebook facebook-php-sdk

我正在尝试在成功登录新网址后重定向我的网页。

<?php
      include_once "fbmain.php";
    $config['baseurl']  =   "http://localhost:8080/index.php";;

    // login or logout url will be needed depending on current user state.
    if ($fbme) {
      $logoutUrl = $facebook->getLogoutUrl(
        array(
            'next'      => $config['baseurl'],
        )
      );
    } else {
      $loginUrl = $facebook->getLoginUrl(
        array(
            'display'   => 'popup',
            'next'      => $config['baseurl'] . '?loginsucc=1',
            'cancel_url'=> $config['baseurl'] . '?cancel=1',
            'req_perms' => 'email,user_birthday',
        )
      );
    }

    // if user click cancel in the popup window
    if (isset($_REQUEST['cancel'])){
        echo "<script>
            window.close();
            </script>";
    }

    if ($fbme && isset($_REQUEST['loginsucc'])){
        //only if valid session found and loginsucc is set

        //after facebook redirects it will send a session parameter as a json value
        //now decode them, make them array and sort based on keys
        $sortArray = get_object_vars(json_decode($_GET['session']));
        ksort($sortArray);

        $strCookie  =   "";
        $flag       =   false;
        foreach($sortArray as $key=>$item){
            if ($flag) $strCookie .= '&';
            $strCookie .= $key . '=' . $item;
            $flag = true;
        }

        //now set the cookie so that next time user don't need to click login again
        setCookie('fbs_' . "{$fbconfig['appid']}", $strCookie);

        echo "<script>
            window.close();
            window.opener.location.reload();
            </script>";
    }

    //if user is logged in and session is valid.
    if ($fbme){
       header("Location: http://localhost:8080/main.php");
       exit;
    }

?>

但是,它会在窗口登录弹出框中加载页面,为什么会这样,以及如何加载它以使其位于主页面中。

3 个答案:

答案 0 :(得分:0)

我使用facebook javascript api做了这个。

FB.init({
appId: '<?= FACEBOOK_APP_ID ?>', 
status: true,
cookie: true, xfbml: true
});
FB.Event.subscribe('auth.login', function(response) {
window.location.href='/app/home.php';
}); 

答案 1 :(得分:0)

更改

if ($fbme){
       header("Location: http://localhost:8080/main.php");
       exit;
    }

到脚本的开头

您还可以取出display' => 'popup',并设置重定向uri,以便人们安装应用程序然后返回原始网站,这样他们就会在成功安装后重定向。

答案 2 :(得分:0)

“next”不起作用,但“redirect_uri”可以完成这项工作!

$params = array( 'redirect_uri' => 'http://yourdomain/fb_api/login_success.php' );
$loginUrl = $facebook->getLoginUrl($params);