简体   繁体   中英

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. The problem I am having is that facebook leaves my site, gets permission from user and facebook and then comes back to my site. 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# =

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.

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?

Got it!! Simple PHP solution...

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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