我有一個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的數據頁面被加載
呃,可能是我錯過了一些東西,但爲什麼你根本就不在你的process_facebook.php中使用header()函數?檢查http://stackoverflow.com/questions/768431/how-to-make-a-redirect-in-php瞭解更多詳情。 – Vick
header('Location:you_url_here');每當你需要通過PHP重定向時使用此方法 –
@Vick我嘗試使用標題,但不是重定向到另一個URL,而是獲取新配置文件頁面的數據並顯示www.example.com/app/signup.php url。如果我重新加載頁面,然後我收到舊的註冊頁面的數據 – Sam