duan89197 2011-08-27 10:20
浏览 97

为什么我收到“页面未正确重定向”错误?

I got this from nettuts, can someone please tell me why am I getting a "the page isn't redirecting properly" error?

<?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);
}
  • 写回答

2条回答 默认 最新

  • dongwu5801 2011-08-27 10:32
    关注

    You redirect authenticated user to $facebook->getLoginUrl(array(...)), creating a redirect loop.

    You should redirect only unauthenticated users (redirect with req_perms should be in else clause). The redirect in try should happen only if you detect that user hasn't granted you all required permissions.

    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
    
    评论

报告相同问题?

悬赏问题

  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程