2014-12-04 101 views
0

我正在創建一個用戶登錄到我的頁面上,並使用了使用PHP登錄的選項。我遇到的問題是Facebook離開我的網站,從用戶和Facebook獲得許可,然後回到我的網站。一切正常,因爲它應該,但令牌的Facebook會創建存儲在URL,如www。############ /帳號/ register.php?代碼= AQDEN90jFQLsDPS5UEXOWCBItXfs5uVbKDs2AfZmV5d7nIHTNy6IXUXtDX-BJMxh6wlsC6-QoOzxnGCLqD_lG4Ui0HF5eUwLP15fUrAgwrwDLHcmRUKrvIcgHOOxr61u6E8me1EV7-VAHiRXG- d-U7qi8fWS7fYpU5OOjGctdzoOvx9Vl35NpXbbALq &狀態= 5659508109f2207707c8#=如何清除Facebook登錄重定向後的URL .. PHP

如果用戶刷新重定向,它的螺絲了整個事情了,因爲它再次重新提交令牌會拋出PHP Facebook的錯誤和結束碼串後頁面。

我的代碼如下。

//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>'; 
} 

所以,我怎麼能在$ SESS變量請求令牌存儲在從Facebook的回報,然後清除網址,使用戶不能在URL中該字符串刷新?

回答

0

明白了!簡單的PHP解決方案...

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