简体   繁体   English

如何在Facebook登录重定向后清除URL。PHP

[英]How to clear URL after Facebook login redirect.. PHP

I am creating a user log in on my page with the option to use facebook to log in using the PHP SDK. 我正在页面上创建用户登录,并可以选择使用Facebook使用PHP SDK登录。 The problem I am having is that facebook leaves my site, gets permission from user and facebook and then comes back to my site. 我遇到的问题是Facebook离开了我的网站,获得了用户和Facebook的许可,然后又回到了我的网站。 Everything works as it should, but the token facebook creates is stored in the url, such as www.############/account/register.php?code=AQDEN90jFQLsDPS5UEXOWCBItXfs5uVbKDs2AfZmV5d7nIHTNy6IXUXtDX-BJMxh6wlsC6-QoOzxnGCLqD_lG4Ui0HF5eUwLP15fUrAgwrwDLHcmRUKrvIcgHOOxr61u6E8me1EV7-VAHiRXG-D-U7qi8fWS7fYpU5OOjGctdzoOvx9Vl35NpXbbALq&state=5659508109f2207707c8# = 一切正常,因为它应该,但令牌的Facebook会创建存储在URL,如www。############ /帐号/ register.php?代码= AQDEN90jFQLsDPS5UEXOWCBItXfs5uVbKDs2AfZmV5d7nIHTNy6IXUXtDX-BJMxh6wlsC6-QoOzxnGCLqD_lG4Ui0HF5eUwLP15fUrAgwrwDLHcmRUKrvIcgHOOxr61u6E8me1EV7-VAHiRXG- D-U7qi8fWS7fYpU5OOjGctdzoOvx9Vl35NpXbbALq&state = 5659508109f2207707c8# =

If the user refreshes that page after being redirected, its screws the whole thing up because it is resubmitting the token again which throws php facebook errors and ends the code chain. 如果用户在重定向后刷新该页面,则它会使整个事情搞砸,因为它再次重新提交了令牌,这引发了php facebook错误并结束了代码链。

My code is below. 我的代码如下。

//1.Use app id,secret and redirect url
 $app_id = '###################';
 $app_secret = '#########';
 $redirect_url='http://#####account/register.php';

 //2.Initialize application, create helper object and get fb sess
 FacebookSession::setDefaultApplication($app_id,$app_secret);
 $helper = new FacebookRedirectLoginHelper($redirect_url);
 $sess = $helper->getSessionFromRedirect();

     //check if facebook session exists
if(isset($_SESSION['fb_token'])){
    $sess = new FacebookSession($_SESSION['fb_token']);
}
    //logout
$logout = 'http:/########/includes/fblogin/lib/Facebook/FbLogout.php';
//3. if fb sess exists echo name 
    if(isset($sess)){
                //store the token in the php session
    $_SESSION['fb_token']=$sess->getToken();
        //create request object,execute and capture response
    $request = new FacebookRequest($sess, 'GET', '/me');
    // from response get graph object
    $response = $request->execute();
    $graph = $response->getGraphObject(GraphUser::className());
    $name = $graph->getName();
    $id = $graph->getId();
    $image = 'https://graph.facebook.com/'.$id.'/picture';
    $email = $graph->getProperty('email');
    echo "hi $name <br>";
    echo "your email is $email <br><Br>";
    echo "<img style='width: 50px; border-radius: 4px;' src='$image' /><br><br>";
    echo "<a href='".$logout."'>Logout</a>";
}else{
    //else echo login
    echo '<a href='.$helper->getLoginUrl().'>Login with facebook</a>';
}

So, how can I store the request token in the $sess variable on return from facebook, and then clear the url so that the user cant refresh with that string in the url? 那么,如何从Facebook返回时将请求令牌存储在$ sess变量中,然后清除url,以使用户无法在url中使用该字符串刷新?

Got it!! 得到它了!! Simple PHP solution... 简单的PHP解决方案...

 if (!empty($_GET)){
    $_SESSION['got'] = $_GET;
    header("Location: http://##############/account/register.php");
}
else{
    if (!empty($_SESSION['got'])){
        $_GET = $_SESSION['got'];
        unset($_SESSION['got']);
    }

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

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