【问题标题】:redirect to a new url after backend script后端脚本后重定向到新的 url
【发布时间】:2015-03-15 01:21:37
【问题描述】:

我有一个 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 页面的数据就会被加载

【问题讨论】:

  • 嗯,可能是我遗漏了一些东西,但为什么你根本不在你的 process_facebook.php 中使用 header() 函数?查看stackoverflow.com/questions/768431/…了解更多详情。
  • header('位置:you_url_here');每当您需要通过 PHP 重定向时使用它
  • @Vick 我尝试使用标头,但不是重定向到另一个网址,而是获取新个人资料页面的数据并显示 www.example.com/app/signup.php 网址。如果我重新加载页面,那么我将获取旧注册页面的数据
  • @MurtazaHussain 我无法获得所需的数据,请阅读我在上面发布的评论
  • ajax请求成功后你有没有尝试使用window.location.href

标签: javascript php jquery ajax facebook


【解决方案1】:

在你的函数文件中粘贴下面的函数:-

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()。

【讨论】:

    【解决方案2】:

    在将任何 HTML 输出到客户端之前,标题需要位于页面顶部。您还可以在服务器向下页面“写入”HTML 到客户端时输出数据。

    在任何 HTML 之前,但在打开 PHP 之后使用:

    header('Location: http://www.example.com/');
    

    参考:http://php.net/manual/en/function.header.php

    【讨论】:

      【解决方案3】:

      如果我没听错的话,您正在使用 Facebook 进行用户会话,并且您使用的所有 Facebook 代码都是 Javascript,因此您想使用 javascript 进行重定向...

      document.location = '/profile.php';
      

      这需要在对后端的调用完成的任何地方实施。

      【讨论】:

        【解决方案4】:

        只需在标题调用后应用die();

        【讨论】:

        • 虽然这回答了这个问题,但在你的答案中加入一些文字来解释你在做什么总是一个好主意。阅读how to write a good answer
        【解决方案5】:

        正如 Harry 所说,你肯定会想通过 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 将其发送到您的后端。您的后端应该解析数据,创建会话详细信息,最重要的是,虽然它会向您发送成功(布尔)和 errorMess(如果适用),然后您使用 .done 来解析成功/失败并呈现消息。这将使客户保持在注册页面上,除非 facebook 的东西是成功的。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2013-07-20
          • 1970-01-01
          • 2017-12-07
          • 2018-11-26
          • 2011-06-05
          • 2015-09-08
          • 1970-01-01
          相关资源
          最近更新 更多