我有一个注册.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并在后端处理数据。 注册.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数据,然后重定向回注册.php页面并显示process_facebook.php页面打印的数据(可选)。我想要的是,与其从process_facebook.php页面重定向回注册.php页面并显示其数据,我希望重定向到另一个页面,不仅新数据应该从新页面加载,而且 url 也应该被更改。(即 www.example.com/app/signup.php 应更改为 www.example.com/app/profile.php)
我忘了提,但我尝试使用 header() 重定向,但它只是获取配置文件.php页面的数据,但显示 www.example.com/app/signup.php URL。现在的问题是,如果我出于任何原因刷新页面,那么旧注册.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进行检查后,您可以通过上述函数重定向用户,此函数将检查标头是否已发送,然后它将使用javascript,否则它将使用用户Header()。
在将任何 HTML 输出到客户端之前,标头需要位于页面顶部。 您还可以在服务器向下将 HTML "写入"客户端时输出数据。
在任何 HTML 之前,但在打开 PHP 之后使用以下命令:
header('Location: http://www.example.com/');
参考: http://php.net/manual/en/function.header.php
没看错的话,你正在使用Facebook进行用户会话,你使用的所有Facebook代码都是Javascript,所以你想使用javascript来做重定向......
document.location = '/profile.php';
这需要在后端调用完成的地方实施。
只需在标头调用后应用die();
即可。
正如哈利所说,你肯定会想通过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的东西是成功的。