用facebook登录网站后重定向页面

时间:2014-01-14 10:03:47

标签: php facebook facebook-graph-api header

我在网站登录facebook后重定向页面时遇到问题。
1.点击网站上的facebook登录按钮,重定向到facebook登录页面
2.用户获得身份验证,但不会重定向到headrer('');中给出的下一页 3.如果我刷新同一页面,则会重定向到指定位置,网址看起来像=“> http://192.1.1.1/mysite/welcome.php/# =
4.它适用于google.com但不适用于本地网址和Google网址的网址看起来像incorrect google url 我的代码

if(isset($_GET['fb']))
{
   $token_url = "https://graph.facebook.com/oauth/access_token?"."client_id=".$config['App_ID']."&redirect_uri=".urlencode($config['callback_url'])."&client_secret=".$config['App_Secret']."&code=".$_GET['code'];   
   $response = file_get_contents($token_url); 
   $params = null;
   parse_str($response, $params);

   $graph_url = "https://graph.facebook.com/me?access_token=".$params['access_token'];
   $user = json_decode(file_get_contents($graph_url));  //user data from facebook

   $email = $user->{'email'};    //i am getting email id from json data

   //search out for entry available for email in database
   $val_user = "SELECT email FROM tbl_facebook WHERE email ='".$email."'";
   $res_val_user = $con->query($val_user);
   $count = mysqli_num_rows($res_val_user);
   // if not then insert data to database
   if($count==0)
   {
       $ins_fb = "INSERT INTO tbl_facebook set email='".$email."'";
       $res_fb = mysqli_query($con,$ins_fb);
       if($res_fb)
       {
          //echo "facebook data saved"; exit; //working for this   
          //header('Location:https://www.google.co.in/'); //working for this also
          header('Location:http://192.1.1.1/mysite/welcome.php');
       }
       else
       {
          echo "facebook data is not saved";
       }
   }
   else // if yes then redirect to welcome page
   {
        //echo "login successfully"; exit; // working for this also 
        header('Location:http://192.1.1.1/mysite/welcome.php');  // getting problem to redirect
        //header('Location:https://www.google.co.in/'); 
   } 
}



<?php
  // code for facebook login

 require 'lib/facebook/src/config.php';
 require 'lib/facebook/src/facebook.php';
// Create our Application instance (replace this with your appId and secret).
$facebook = new Facebook(array(
      'appId'  => $config['App_ID'],
      'secret' => $config['App_Secret'],
      'cookie' => true
));
?>



<a class="btn-facebook" href="https://www.facebook.com/dialog/oauth?client_id=<?php echo $config['App_ID']?>&redirect_uri=<?php echo $config['callback_url']?>&scope=email,user_likes,publish_stream">
                    <i class="icon-facebook icon-large"></i>
                    Signin with Facebook
                </a>

2 个答案:

答案 0 :(得分:0)

标头重定向问题通常是在PHP关闭标记之后或开始标记之前有空格,在标题()之前输出等。

为避免这种情况,我们需要确保

  • 没有提到的空格
  • 在此之前不要回复任何事情。

此外,即使header()不起作用,也可以将exit()添加为

header('Location:http://192.1.1.1/mysite/welcome.php');
exit();

最后你可以使用JS作为

echo '<script>window.location = "'.$url.'";</script>';
exit;

希望这会有所帮助:)

答案 1 :(得分:0)

只使用本地路径网址不要使用完整网址

header("Location: welcome.php");