简体   繁体   中英

Log Out of Website when User Logs Out of Facebook

I am using the Facebook Connect PHP SDK to allow users to log into my website:

try {
    $facebook = new Facebook(array(
      'appId'  => $fbconfig['appid'],
      'secret' => $fbconfig['secret'],
      'cookie' => true,
    ));
} catch(Exception $o) {
    print_r($o);    
}

// Facebook Authentication part
$user       = $facebook->getUser();    
$loginUrl   = $facebook->getLoginUrl(
                array(
                    'scope'         => 'email,user_likes,publish_actions,read_stream,friends_likes,user_checkins,user_location,friends_checkins,user_status',
                    'redirect_uri'  => $fbconfig['baseurl'].'load-data.php',
                    'cancel_url'    => $fbconfig['baseurl'].'/index.php'
                )
            );

The login URL is created, and the user is logged into the site successfully. The problem is that when the user logs out of Facebook in another tab, I want to get this status and log the user out from my program using the Facebook SDK.

How can I log the user out when they log out of Facebook?

Thanks for reading my question.

I don't think you can find out when the user logged out from Facebook without messing up with the FB Session directly as this is different from your app Session. And I don't thing this would be a good think to do.

A different approach would be to logout the user from your app but I don't know if it would solve your problem.

        <script type="text/javascript">

        var int=self.setInterval(function(){checkSession()},1000);

        function checkSession() {
             var request = false;
                if(window.XMLHttpRequest) { // Mozilla/Safari
                    request = new XMLHttpRequest();
                } else if(window.ActiveXObject) { // IE
                        request = new ActiveXObject("Microsoft.XMLHTTP");
                }

                request.open('get', 'checksession.php', true);
                request.send();
                request.onreadystatechange = function() {

                    if(request.readyState == 4) 
                    {
                        var session = request.responseText;
                         if(session=="false")  {
                            alert("Your Session has expired");
                            window.location = "login.php";
                         }
                    }
                }
                request.send(null);
            }

            </script>

php script

    $user = $facebook->getUser();

    if ($user) {
      echo "false";
    } else {
      echo "true";
    }

Can't logout using Facebook API

I think this maybe helpful.

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