简体   繁体   中英

If condition is always returning zero while getting the user in " $this->'library_name'->getUser()

I am trying this code :

$this->load->library('fbconnect');

    if ($this->fbconnect->user){

         echo "<pre>";  
         print_r($this->fbconnect->user);
         echo "</pre>";
    }else{
         echo "could not login at this tym";
     }

It is always displaying the echo part of 'else' condition. 'fbconnect' is the library name containing facebookconnect code and 'user' is getting the value from fb function 'getUser()'

Here it is: the code of 'fbconnect.php' library:

class Fbconnect extends Facebook{

    public  $user = NULL;
    public  $user_id = NULL;

    public function Fbconnect(){

        $ci=& get_instance();       
        $ci->config->load('facebook',TRUE);     
        $config = $ci->config->item('facebook');        
        parent::__construct($config);

            $this->user_id = $this->getUser();
        $me=NULL;


        if($this->user_id){
            try{
                $me = $this->api('/me');
                $this->user = $me;
            }
            catch(FacebookApiException $e){
                error_log($e);
            }
        } 
    }
}

The login function can be put in controller itself. Add Facebook.php(facebook php sdk file) and base_facebook.php(facebook php sdk file) to application/libraries

class Fbconnect extends CI_Controller{
    public function Fbconnect()
    {
       parent::__construct();
       $CI = & get_instance();
       $CI->config->load("facebook",TRUE);
       $config = $CI->config->item('facebook');
       $this->load->library('Facebook', $config);
    }

    function facebook_login()
    {
        $me = $this->facebook->getUser();

        if($me == 0){
        $loginUrl = $this->facebook->getLoginUrl();
        //Do the necessary with $loginUrl
        }else{
              try{
                   $user = $this->facebook->api('/me');
                   return $user; //or echo $user
                 }catch(FacebookApiException $e)
                 {
                   error_log($e);
                 }
      } 
   }
}

Now call the function ie http://yoursite.com/index.php/Fbconnect/facebook_login

check log file or use

if($this->user_id){
        try{
            $me = $this->api('/me');
            $this->user = $me;
        }
        catch(FacebookApiException $e){
            echo $e->getMessage();
        }
    } 

to see error message.

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