How to log out from facebook in my site

☆樱花仙子☆ 提交于 2019-12-01 07:18:25

问题


I'm using https://github.com/pocesar/facebook-kohana for facebook login. I have problem with facebook logout. It doesn't destroy facebook session. I've tried so many things and I've read many questions. I tried this in my logout method, but no result: $this->redirect('https://www.facebook.com/logout.php? next=mysite.dev &access_token=USER_ACCESS_TOKEN');

My logout method is:

  public  function action_logout(){
         $facebook = new Facebook(array(
            'appId'  => 'appId',
            'secret' => 'mySecret',
        ));
         $user = $facebook->getUser();

        $facebook->destroySession();
        Session::instance()->delete('user');
     
        $this->redirect('/');
    }

How to destroy session, so that user can log in my site with another facebook account? Thanks!

My method login is:

 public function action_fbLogin(){
      
        $facebook = new Facebook(array(
            'appId'  => 'appId',
            'secret' => 'Secret',
        ));

        $user = $facebook->getUser();
       
        if ($user) {

            $user_profile = $facebook->api('/me', array('fields' => 'id,email,name,first_name,last_name,picture')); 
          
            $user_id = Model_UserFunctions::checkIfUserExist($user_profile['email']); 
            if($user_id > 0)
            {
                
                Session::instance()->set('user', array(
                    'fb_id' => $user_profile['id'],
                    'user_id' => $user_id,
                    'pic' => $user_profile['picture'],
                    'email' => $user_profile['email'],
                    'first_name' =>  $user_profile['first_name'],
                    'last_name' =>  $user_profile['last_name'],
                ));
                
                //var_dump($_SESSION);
                $this->redirect('profile');
                exit;
            }
            $values = array(
                    'email' => $user_profile['email'],
                    'username' => $user_profile['email'],
                    'password' => '12345678'
                );
            $user = ORM::factory ( 'User' );
            $user->values($values);
            try
            {
                if($user->save()){
                    $user_new_id = $user->as_array();
                    $user_new_id = $user_new_id['id'];
                    Session::instance()->set('user', array(
                            'fb_id' => $user_profile['id'],
                            'user_id' => $user_new_id,
                            'pic' => $user_profile['picture'],
                            'email' => $user_profile['email'], 
                            'first_name' => $user_profile['first_name'],
                            'last_name' => $user_profile['last_name'],
                        ));
                    
                    $this->redirect('profile');
                }


            }
            catch (ORM_Validation_Exception $e)
            {
                $result = $e->errors('models');
                echo '<pre>';
                print_r($result);
                exit;
            }


        }
        else 
        {
            
            $this->redirect($facebook->getLoginUrl(array('id,email,name,first_name,last_name,picture'))); 
        }
        exit;
    
    }

Edited: My goal to use Facebook logout is because in my site I only use Facebook login, there isn't another way to log in my site, that's idea of it. And I should have logout method in my site, so when user wants to logout, he could do it. This logouts me from facebook but facebook login page is shown. How to redirect next to my site. I've set it as next but it doesn't redirect to it:

<a href="https://www.facebook.com/logout.php? next=http://mysite.dev &access_token=Null">Logout</a>


回答1:


You can get facebook logout url using facebook-php-sdk API. When user will click on that url he will be offline from his facebook account.

if ($user) {
   $logoutUrl = $facebook->getLogoutUrl();
} else {
   $loginUrl = $facebook->getLoginUrl();
}

For more info you can check this url.

https://github.com/facebookarchive/facebook-php-sdk




回答2:


Then you don't have a problem with facebook

Is only the session that needs to be destroyed...

On this page of PHP manual there is an example showing you might need aditional steps to destroy a session beyond unset($_SESSION)

http://php.net/manual/en/function.session-destroy.php

Forcing the users to log off from Facebook does not seem a good move for me.

Millions of FB users have intentionally asked to stay logged in and if they visit your site and logoff from there they might think it wasn't very friendly of you to log them off the FB accounts. many don't even remember their pwd... so you would be creating a problem for them and they might never comeback to your site... Just saying



来源:https://stackoverflow.com/questions/32819093/how-to-log-out-from-facebook-in-my-site

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!