简体   繁体   中英

not able to redirect after facebook login

I am able to signup with facebook javascript sdk and successfully stored fb login info into WP_user table , but further it is not redirecting to landing page

this is my code after sucessful fb login

$info1 = array();
    $info1['user_login'] = $info['user_login'];
    $info1['user_password'] =  $info['user_pass'];
    $info1['remember'] = true;

    $user = wp_signon( $info1, false );


        echo json_encode(array('loggedin'=>true, 'message'=>__($login.' successful, redirecting...')));
        wp_redirect(home_url());

this is the Code on header.php to check if user is logged in

 <?php if (is_user_logged_in()) { ?>
 <a href="<?php echo wp_logout_url( home_url() ); ?>">Logout</a>
 <?php } else { 

 ?>             
        <a>Login</a>
        <a>register</a>

<?php } ?>

: I also tried one more script for landing Page with login and show welcome

user after login

              <?php

// Enter the app id and secret below
define('YOUR_APP_ID', 'xxxxxxxxxxxx');
define('YOUR_APP_SECRET', 'xxxxxxxxxxx');

function get_facebook_cookie($app_id, $app_secret) {
  $args = array();
  parse_str(trim($_COOKIE['fbs_' . $app_id], '\\"'), $args);
  ksort($args);
  $payload = '';
  foreach ($args as $key => $value) {
    if ($key != 'sig') {
      $payload .= $key . '=' . $value;
    }
  }
  if (md5($payload . $app_secret) != $args['sig']) {
    return null;
  }
  return $args;
}

$cookie = get_facebook_cookie(YOUR_APP_ID, YOUR_APP_SECRET);
//$user = json_decode(file_get_contents('https://graph.facebook.com/me?access_token='.$cookie['access_token'//])

$user = viacurl('https://graph.facebook.com/me?access_token='.$cookie['access_token']);

function viacurl($location){
    $ch = curl_init();

    curl_setopt($ch, CURLOPT_URL, $location);
    curl_setopt($ch, CURLOPT_HEADER, false);

    $out=curl_exec($ch);

    curl_close($ch);

    return rtrim($out,1);
}

//);

?>

    <div id="fb-root"></div>
    <?php 

    print_r($user);

    if ($user->id) { ?>
      <p>Welcome <?= $user->name ?></p>
    <?php } else { ?>
      <script src="http://connect.facebook.net/en_US/all.js"></script>
       <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>

      <script>
        FB.init({ 
          appId:<?php print YOUR_APP_ID; ?>, cookie:true, 
          status:true, xfbml:true
        });
      </script>
      <fb:login-button>Login to Eureka!</fb:login-button>
    <?php } ?>


  </div>

but this also seem obselete

I have seen some posts in which it is said that wordpress take care of session itself .

DO i need to use php sessions or wordpress can do it on its own ?? please suggest if missing something ...

Note: wp_redirect() does not exit automatically, and should almost always be followed by a call to exit;:

wp_redirect(home_url());
exit;

wp_redirect should always be included above the get_header();

I suggest to include this with a add_action init. https://codex.wordpress.org/Plugin_API/Action_Reference/init

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM