我有一个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并在后端处理数据.用于注册页面中的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之后,你可以通过上面的功能重定向用户,这个功能会检查是否已经发送了头,然后它会使用javascript,否则会使用Header().