【问题标题】:unable to to open redirected page after facebook authenticationfacebook认证后无法打开重定向页面
【发布时间】:2012-06-11 12:13:32
【问题描述】:

我在 codeigniter 下使用 facebook connect。身份验证后我想重定向控制器的成功方法 这是我的控制器:

class Welcome extends CI_Controller {
function __construct()
{
    parent::__construct();

    $this->load->model('Facebook_model');
}

function index()
{
    $fb_data = $this->session->userdata('fb_data');

    $data = array(
                'fb_data' => $fb_data,
                );

    $this->load->view('welcome', $data);
}

function topsecret()
{
    $fb_data = $this->session->userdata('fb_data');

    if((!$fb_data['uid']) or (!$fb_data['me']))
    {
        redirect('welcome');
    }
    else
    {
        $data = array(
                    'fb_data' => $fb_data,
                    );

        $this->load->view('topsecret', $data);
    }
}
function success()
{
    $this->load->view('welcome_message');
}
}

我的 facebook api 访问模型:

class Facebook_model extends CI_Model {
public function __construct()
{
    parent::__construct();
    $config = array(
                    'appId'  => '261066574000678',
                    'secret' => '   79e11f65449988965362f58e9a4aabd7',
                    'fileUpload' => true, // Indicates if the CURL based @ syntax for file uploads is enabled.
                    );

    $this->load->library('Facebook', $config);

    $user = $this->facebook->getUser();

    // We may or may not have this data based on whether the user is logged in.
    //
    // If we have a $user id here, it means we know the user is logged into
    // Facebook, but we don't know if the access token is valid. An access
    // token is invalid if the user logged out of Facebook.
    $profile = null;
    if($user)
    {
        try {
            // Proceed knowing you have a logged in user who's authenticated.
            $profile = $this->facebook->api('/me?fields=id,name,link,email');
        } catch (FacebookApiException $e) {
            error_log($e);
            $user = null;
        }       
    }

    $fb_data = array(
                    'me' => $profile,
                    'uid' => $user,
                    'loginUrl' => $this->facebook->getLoginUrl(
                        array(
                            'scope' => 'email,user_birthday,publish_stream', // app permissions
                            'redirect_uri' => 'https://sanjay.localhost.com/index.php/welcome/success' // URL where you want to redirect your users after a successful login
                    )
                    ),
                    'logoutUrl' => $this->facebook->getLogoutUrl(),
                );

    $this->session->set_userdata('fb_data', $fb_data);
}
}

因为我在 localhost 主机上对此进行测试,所以我还编辑了我的主机文件并将我的 localhost 主机名更改为 sanjay.localhost.com.redirect 发生但没有发生..我认为可能是因为 querystring.when 重定向发生重定向 uri是
=">https://sanjay.localhost.com/index.php/welcome/success?state=ff5712299510defa&code=AQCaD-FAd1shuW#=
我不明白如何处理查询字符串中的状态和代码。 请帮忙。

【问题讨论】:

  • 它是否重定向回欢迎/成功?使用 facebook 进行身份验证后,发生了什么?是否显示任何错误?
  • @Gavin 重定向发生,但显示此网页不可用,浏览器地址如下:sanjay.localhost.com/index.php/welcome/… dnt 知道如何处理此查询字符串
  • 查看uri_protocol 文件中的uri_protocol 设置。尝试将其设置为每个可用值(有关可用选项,请参阅上面的 cmets),然后转到失败的 URL。我在我的所有网站上都使用 CodeIgniter,在混合用户友好的 url 和包含查询字符串值的 url(即通过查询字符串返回值的贝宝付款)时,我从来没有遇到过问题,所以只需找出有效的方法;)
  • @Gavin ya 但是如何处理这个查询字符串值(状态和代码)。如何在我的成功方法中处理它。请告诉我。我是新手。:(
  • 我按照你说的试过了。还是不行:(

标签: facebook codeigniter facebook-graph-api session-state


【解决方案1】:

感谢您在我的博客上与我联系。首先,Facebook 停止了对 localhost 的支持。她是链接https://developers.facebook.com/bugs/128794873890320

我还没有使用 codeigniter 开发过任何应用程序,我使用的是 CakePHP,但 auth follow 应该是一样的。
1.在用户控制器中创建一个fb_login函数。
2. 这个函数会遵循这个逻辑。 一种。使用 $facebook->getUser() 获取用户 ID。 湾。然后使用$facebook->api('/me') 确定。
3.如果你得到 FacebookApiException 然后发送用户使用 Facebook 登录。如果您使用官方 SDK,则当前 url 将被添加到重定向 url。
4.Facebook 将在登录后重定向您的用户。因此您将使用$facebook->getUser() 获取数据。将此数据保存在会话中,以便在您的应用程序中进一步使用。然后将用户重定向到您的控制页面或任何其他页面。 CakePHP 有setFlash() 函数,它可以显示在控制面板中设置的任何消息。我认为 Codeignator 应该有这样的东西。如果没有,您可以简单地在会话中设置一个消息并重定向用户。然后在显示味精后取消设置味精。

这是完整的代码

$uid = $facebook->getUser();        
try {
    $user_profile = $facebook->api('/me');
} catch (FacebookApiException $e) { 
    //echo $e->getMessage();
    $uid = null;
}

$loginUrl   = $facebook->getLoginUrl(
        array(
            'scope'         => 'publish_stream,offline_access,email'
        ),''
);

if (!$uid) {
    echo "<script type='text/javascript'>top.location.href = '$loginUrl';</script>";
    exit;
}
//search using uid on your user table to check if the use is returnign user or new user.
if($new_user ==1)
{
    $this->Session->setFlash('please sign up');
    //redirect to sign up
}
else
{
    $this->Session->setFlash('you are good lad');
    //reditect to control panel
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-30
    • 2020-02-26
    • 2012-03-27
    • 2019-10-29
    • 1970-01-01
    相关资源
    最近更新 更多