我有一个 signup.php 页面,其中包含通过 facebook 按钮登录。页面结构是这样的
<?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 页面并显示其数据,不仅应该从新页面加载新数据,而且还应该获取 url已更改。(即 www.example.com/app/signup.php 应更改为 www.example.com/app/profile.php)
我忘了提,但我已经尝试使用 header() 进行重定向,但它只是获取 profile.php 页面的数据,但显示 www.example.com/app/signup.php url.. 现在问题出在里面是如果我出于任何原因刷新页面,然后加载旧 signup.php 页面的数据
最佳答案
做一件事,将下面的函数粘贴到您的函数文件中:-
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 应用检查后,您可以通过上述函数重定向用户,此函数将检查 header 是否已发送,然后它将使用 javascript,否则它将使用 Header()。
关于javascript - 在后端脚本后重定向到一个新的 url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27983939/