javascript – 重定向到后端脚本后的新URL

前端之家收集整理的这篇文章主要介绍了javascript – 重定向到后端脚本后的新URL前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个signup.PHP页面,其中包含通过Facebook按钮登录.页面的结构是这样的
  1. <?PHP
  2. if(!isset($_SESSION['logged_in']))
  3. {
  4. echo '<div id="results">';
  5. echo '<!-- results will be placed here -->';
  6. echo '</div>';
  7. echo '<div id="LoginButton">';
  8. echo '<a href="#" rel="nofollow" class="fblogin-button" onClick="javascript:CallAfterLogin();return false;">Login with Facebook</a>';
  9. echo '</div>';
  10. }
  11. else
  12. {
  13. echo 'Hi '. $_SESSION['user_name'].'! You are Logged in to facebook,<a href="?logout=1">Log Out</a>.';
  14. }
  15. ?>

页面上使用的ajax代码调用另一个页面process_facebook.PHP并在后端处理数据.用于注册页面中的ajax的代码

  1. <script type="text/javascript">
  2. window.fbAsyncInit = function() {
  3. FB.init({
  4. appId: '<?PHP echo $appId; ?>',cookie: true,xfbml: true,channelUrl: '<?PHP echo $return_url; ?>channel.PHP',oauth: true
  5. });
  6. };
  7. (function() {
  8. var e = document.createElement('script');
  9. e.async = true;e.src = document.location.protocol +'//connect.facebook.net/en_US/all.js';
  10. document.getElementById('fb-root').appendChild(e);}());
  11.  
  12. function CallAfterLogin(){
  13. FB.login(function(response) {
  14. if (response.status === "connected")
  15. {
  16. LodingAnimate(); //Animate login
  17. FB.api('/me',function(data) {
  18.  
  19. if(data.email == null)
  20. {
  21. //Facbeook user email is empty,you can check something like this.
  22. alert("You must allow us to access your email id!");
  23. ResetAnimate();
  24.  
  25. }else{
  26. AjaxResponse();
  27. }
  28.  
  29. });
  30. }
  31. },{scope:'<?PHP echo $fbPermissions; ?>'});
  32. }
  33.  
  34. //functions
  35. function AjaxResponse()
  36. {
  37. //Load data from the server and place the returned HTML into the matched element using jQuery Load().
  38. $( "#results" ).load( "process_facebook.PHP" );
  39. }
  40.  
  41. //Show loading Image
  42. function LodingAnimate()
  43. {
  44. $("#LoginButton").hide(); //hide login button once user authorize the application
  45. $("#results").html('<img src="img/ajax-loader.gif" /> Please Wait Connecting...'); //show loading image while we process user
  46. }
  47.  
  48. //Reset User button
  49. function ResetAnimate()
  50. {
  51. $("#LoginButton").show(); //Show login button
  52. $("#results").html(''); //reset element html
  53. }
  54.  
  55. </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页面的数据

解决方法

函数文件中粘贴以下函数
  1. function redirect($url=NULL)
  2. {
  3. if(is_null($url)) $url=curPageURL();
  4. if(headers_sent())
  5. {
  6. echo "<script>window.location='".$url."'</script>";
  7. }
  8. else
  9. {
  10. header("Location:".$url);
  11. }
  12. exit;
  13. }

而这个函数调用就像:

  1. redirect($url);

所以当你从Facebook获取数据,所以在应用了$user_profile之后,你可以通过上面的功能重定向用户,这个功能会检查是否已经发送了头,然后它会使用javascript,否则会使用Header().

猜你在找的JavaScript相关文章