user1481850
user1481850

Reputation: 248

php-Facebook redirect after registration?

I am using the php sdk in order to login the users to the website. Also I am using the redirect_uri to redirect to login.php I am setting up a cookie with the user id. Then I am checking if the cookie exists I am using the header function in order to dircet the users back to main.php

<?php   if(isset($_COOKIE['id']) == false){

        require_once('scripts/facebook.php');
        $config = array('appId' => 'xxx','secret' => 'xxx');
        $params = array('scope' => 'email,offline_access,user_birthday', 'redirect_uri' => 'http://www.xxx.com/login.php');
        $facebook = new Facebook($config);
        $user = $facebook->getUser(); 

        if($user) {

            try {

            $user_profile = $facebook->api('/me','GET');
        $userid = $user_profile['username'];



                //insert cookie
                $expire = time() + 31556926;
                $cookie_id = $user_profile['username'];
                setcookie("id", $cookie_id, $expire);
                header('Location: main.php');



          } catch(FacebookApiException $e) {
                            $login_url = $facebook->getLoginUrl($params);
                            echo '<img src="assets/login.png"><br>';
                            echo '<a href="' . $login_url . '"><img src="assets/facebook_login.png"></a>';
                            error_log($e->getType());
                            error_log($e->getMessage());
          }   
        } else {

            $login_url = $facebook->getLoginUrl($params);
            echo '<img src="assets/login.png"><br>';
            echo '<a href="' . $login_url . '"><img src="assets/facebook_login.png"></a>';
        }//end facebook
        }else{
            //if cookie id is set
            header('Location: main.php');
            }
    ?>

now, the problem is when the user authenticated the app, it is redirecting to the login page. which means that the php page is not getting refreshed. How can I solve this problem ? UPDATE: if the user refresh the page or click login again he will be automatically redirected to main ?

Upvotes: 0

Views: 288

Answers (1)

easyman
easyman

Reputation: 11

I have the same issue here. I redirect the website user to login using the Facebook site via the Facebook PHP function getLoginURL(). Once a user has gone to login screen at Facebook and been redirected back to my site, the page does not refresh. This means that I cannot update the page display for the now currently logged-on user. This seems a bit daft - because I would have thought that would be the obvious 'default' thing to do.

If I/you add the client-side Facebook JS and root div, then the process works with a Facebook JS driven page reload - but this reload based on pulling in the client-side Facebook JS is a dependency I could do without - it just adds bloat and complicates things. Anyone with any other ideas on how to get around this.

The client-side JS is placed just before your closing body tag:

<div id="fb-root"></div>
<script>
  window.fbAsyncInit = function() {
    FB.init({
      appId: '<?php echo $facebook->getAppID() ?>',
      cookie: true,
      xfbml: true,
      oauth: true
    });
    FB.Event.subscribe('auth.login', function(response) {
      window.location.reload();
    });
    FB.Event.subscribe('auth.logout', function(response) {
      window.location.reload();
    });
  };
  (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);
  }());
</script>

Upvotes: 1

Related Questions