简体   繁体   English

为什么会出现“页面无法正确重定向”错误?

[英]Why am I getting a “the page isn't redirecting properly” error?

I got this from nettuts, can someone please tell me why am I getting a "the page isn't redirecting properly" error? 我是从nettuts那里得到的,有人可以告诉我为什么我收到“页面无法正确重定向”的错误吗?

<?php
# We require the library
require("facebook.php");

# Creating the facebook object
$facebook = new Facebook(array(
    'appId'  => 'APP_ID_HERE',
    'secret' => 'APP_SECRET_HERE',
    'cookie' => true
));

# Let's see if we have an active session
$session = $facebook->getUser();

if(!empty($session)) {
    # Active session, let's try getting the user id (getUser()) and user info (api->('/me'))
    try{
        $uid = $facebook->getUser();

        # req_perms is a comma separated list of the permissions needed
        $url = $facebook->getLoginUrl(array(
            'req_perms' => 'email,user_birthday,status_update,publish_stream,user_photos,user_videos'
        ));
        header("Location: $url");
    } catch (Exception $e){}
} else {
    # There's no active session, let's generate one
    $login_url = $facebook->getLoginUrl();
    header("Location: ".$login_url);
}

You redirect authenticated user to $facebook->getLoginUrl(array(...)) , creating a redirect loop. 您将通过身份验证的用户重定向到$facebook->getLoginUrl(array(...)) ,从而创建一个重定向循环。

You should redirect only unauthenticated users (redirect with req_perms should be in else clause). 您应该只重定向未经req_perms验证的用户(使用req_perms进行的重定向应该在else子句中)。 The redirect in try should happen only if you detect that user hasn't granted you all required permissions. 仅当您检测到该用户未授予您所有必需的权限时,才应进行try重定向。

You can check granted permissions by invoking: 您可以通过调用以下方法检查授予的权限:

$perms = $facebook->api(array(
    'method' => 'fql.query',
    'query' => 'SELECT email,user_birthday,status_update,publish_stream,user_photos,user_videos FROM permissions WHERE uid=' . $facebook->getUser()
));


Modified code: 修改后的代码:

 <?php # We require the library require("facebook.php"); # Creating the facebook object $facebook = new Facebook(array( 'appId' => 'APP_ID_HERE', 'secret' => 'APP_SECRET_HERE', 'cookie' => true )); # Let's see if we have an active session $session = $facebook->getUser(); if(empty($session)) { # There's no active session, let's generate one $url = $facebook->getLoginUrl(array( 'req_perms' => 'email,user_birthday,status_update,publish_stream,user_photos,user_videos' )); header("Location: $url"); exit; } // user is logged in 

If you are using the latest PHP SDK, several changes have been made and the permissions you are asking do not work that way. 如果您使用的是最新的PHP SDK,则已进行了一些更改,而您要求的权限则无法正常工作。

Here is an updated code. 这是更新的代码。

<?php 
# We require the library 
require("facebook.php"); 

# Creating the facebook object 
$facebook = new Facebook(array( 
    'appId'  => 'APP_ID_HERE', 
    'secret' => 'APP_SECRET_HERE', 
    'cookie' => true 
)); 

# Let's see if we have an active session 
$session = $facebook->getUser(); 

if(empty($session)) { 
    # There's no active session, let's generate one 
    $url = $facebook->getLoginUrl(array( 
        "response_type"=>"token", //Can also be "code" if you need to
        "scope" => 'email,user_birthday,status_update,publish_stream,user_photos,user_videos' ,
        "redirect_uri"=> "http://test.com" //Your app callback url
    )); 
    header("Location: $url"); 
    exit; 
} 
// user is logged in 

More info: http://developers.facebook.com/docs/authentication/ 更多信息: http : //developers.facebook.com/docs/authentication/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM