2015-01-16 23 views
10

我有一個signup.php頁面,其中包含通過Facebook的登錄按鈕。頁面的結構是這樣的頁面上使用此後端腳本後重定向到一個新的URL

<?php 
if(!isset($_SESSION['logged_in'])) 
{ 
    echo '<div id="results">'; 
    echo '<!-- results will be placed here -->'; 
    echo '</div>'; 
    echo '<div id="LoginButton">'; 
    echo '<a href="#" rel="nofollow" class="fblogin-button" onClick="javascript:CallAfterLogin();return false;">Login with Facebook</a>'; 
    echo '</div>'; 
} 
else 
{ 
    echo 'Hi '. $_SESSION['user_name'].'! You are Logged in to facebook, <a href="?logout=1">Log Out</a>.'; 
} 
?> 

AJAX代碼調用另一頁process_facebook.php,並在後臺處理數據。在signup.php頁面使用Ajax代碼是

<script type="text/javascript"> 
window.fbAsyncInit = function() { 
    FB.init({ 
     appId: '<?php echo $appId; ?>', 
     cookie: true,xfbml: true, 
     channelUrl: '<?php echo $return_url; ?>channel.php', 
     oauth: true 
     }); 
    }; 
(function() { 
    var e = document.createElement('script'); 
    e.async = true;e.src = document.location.protocol +'//connect.facebook.net/en_US/all.js'; 
    document.getElementById('fb-root').appendChild(e);}()); 

function CallAfterLogin(){ 
    FB.login(function(response) {  
     if (response.status === "connected") 
     { 
      LodingAnimate(); //Animate login 
      FB.api('/me', function(data) { 

       if(data.email == null) 
       { 
        //Facbeook user email is empty, you can check something like this. 
        alert("You must allow us to access your email id!"); 
        ResetAnimate(); 

       }else{ 
        AjaxResponse(); 
       } 

      }); 
     } 
    }, 
    {scope:'<?php echo $fbPermissions; ?>'}); 
} 

//functions 
function AjaxResponse() 
{ 
    //Load data from the server and place the returned HTML into the matched element using jQuery Load(). 
    $("#results").load("process_facebook.php"); 
} 

//Show loading Image 
function LodingAnimate() 
{ 
    $("#LoginButton").hide(); //hide login button once user authorize the application 
    $("#results").html('<img src="img/ajax-loader.gif" /> Please Wait Connecting...'); //show loading image while we process user 
} 

//Reset User button 
function ResetAnimate() 
{ 
    $("#LoginButton").show(); //Show login button 
    $("#results").html(''); //reset element html 
} 

</script> 

現在的問題是,process_facebook.php頁面FB數據工作在後臺,之後被重定向回signup.php頁面並顯示數據process_facebook.php頁面已打印(可選)。我想要的是,而不是從process_facebook.php頁面重定向到signup.php頁面並顯示其數據,我希望重定向到另一個頁面,不僅新的數據應該從新的頁面加載,但該網址也應該得到(即www.example.com/app/signup.php應改爲www.example.com/app/profile.php)

我忘了提及,但我已經嘗試使用header()來重定向,但它只是簡單地獲取profile.php頁面的數據,但顯示www.example.com/app/signup.php網址..現在問題在於,如果我因任何原因刷新頁面,那麼舊signup.php的數據頁面被加載

+0

呃,可能是我錯過了一些東西,但爲什麼你根本就不在你的process_facebook.php中使用header()函數?檢查http://stackoverflow.com/questions/768431/how-to-make-a-redirect-in-php瞭解更多詳情。 – Vick

+0

header('Location:you_url_here');每當你需要通過PHP重定向時使用此方法 –

+0

@Vick我嘗試使用標題,但不是重定向到另一個URL,而是獲取新配置文件頁面的數據並顯示www.example.com/app/signup.php url。如果我重新加載頁面,然後我收到舊的註冊頁面的數據 – Sam

回答

0

在將任何HTML輸出到客戶端之前,頁眉需要放在頁面的頂部。您也可以在服務器關閉「編寫」HTML頁面到客戶端時輸出數據。

header('Location: http://www.example.com/'); 

Referr到:

任何HTML之前,但打開PHP使用這種後http://php.net/manual/en/function.header.php

+0

可以üplz顯示如何去做 – Sam

+0

這是否解決了它? –

0

如果我按照正確的,你使用Facebook的用戶會話和所有的Facebook的代碼,你'使用的是Javascript,所以你想使用JavaScript來做重定向...

document.location = '/profile.php'; 

這需要到位,無論後面的呼叫完成。

0

只需在標頭調用後應用die();即可。

+0

雖然這回答了問題,但在回答中放置一些文字以解釋您正在做的事情總是一個好主意。閱讀[如何寫出一個好答案](http://stackoverflow.com/help/how-to-answer)。 – jurgemaister

0

正如Harry所說,你肯定會想通過JS來做到這一點。

document.location = '/profile.php'; 

function CallAfterLogin(){ 
    FB.login(function(response) {  
     if (response.status === "connected") 
     { 
      LodingAnimate(); //Animate login 
      FB.api('/me', function(FBdata) { 

       if(FBdata.email == null) 
       { 
        //Facbeook user email is empty, you can check something like this. 
        alert("You must allow us to access your email id!"); 
        ResetAnimate(); 

       }else{ 
        //you need to call some php script via ajax here to send these details to your server. 
        $.post('fbResponseParse.php',FBdata) 
        // your php should return some sort of an ok, or even a url for you to redirect to. 
        //if it fails you need to display an error not redirect. 
         .done(function(data){ 
         //data in this scope is the response from your php script. 
          if(data.success) document.location = '/profile.php'; 
          else displayError(data.errorMess) 
         }) 
       } 

      }); 
     } 
    }, 
    {scope:'<?php echo $fbPermissions; ?>'}); 
} 

所以你正在使用JS說話給Facebook做什麼。然後,通過AJAX將其發送到您的後端。您的後端應該解析數據,創建會話詳細信息,最重要的是,儘管它會向您發送成功(bool)和errorMess(如果適用),然後使用.done解析成功/失敗並呈現消息。這將保持客戶端在註冊頁面上,除非Facebook的東西是成功的。

1

做一件事粘貼以下功能在你的函數文件: -

function redirect($url=NULL) 
{ 
    if(is_null($url)) $url=curPageURL(); 
    if(headers_sent()) 
    { 
     echo "<script>window.location='".$url."'</script>"; 
    } 
    else 
    { 
     header("Location:".$url); 
    } 
    exit; 
} 

而調用這個函數,如:

redirect($url); 

所以,當你將獲得來自Facebook的數據,以便應用後檢查$ user_profile你可以通過上面的函數重定向用戶,這個函數將檢查頭是否已經發送,然後它將使用JavaScript,否則它將用戶Header()。