Search code examples
phpfacebookfacebook-graph-apizend-frameworkfacebook-sdk-3.0

facebook SDK incorrect ids being supplied using the getUser method in different locations of zend 1 framework application


I am having difficulty with facebook user ids in our system. After retrieving the information via the facebook api method getUser(), i cannot obtain that id from any subsequent calls to getUser().

Here is the registration code that redirects the user to the facebook login:

    $this->view->layout ()->disableLayout ();
    $this->_helper->viewRenderer->setNoRender ( true );
    $appId = Ranger_Application_Util::getConfig('facebook_app_id');
    $appSecret = Ranger_Application_Util::getConfig('facebook_app_secret');
    require_once 'facebook/src/facebook.php';
    $facebook = new Facebook(array(
            'appId' => $appId,
            'secret' => $appSecret,
            'cookie' => true
    ));
    $args = array();
    $args['redirect_uri'] = 'http://url.com/en/account/callback-facebook';
    $url = $facebook->getLoginUrl($args);
    $redirector = Zend_Controller_Action_HelperBroker::getStaticHelper ( 'Redirector' );
    $redirector->gotoUrl ( $url )->redirectAndExit ();

Here is the redirect code:

    $this->view->layout ()->disableLayout ();
    $this->_helper->viewRenderer->setNoRender ( true );
    $appId = Ranger_Application_Util::getConfig('facebook_app_id');
    $appSecret = Ranger_Application_Util::getConfig('facebook_app_secret');
    require_once 'facebook/src/facebook.php';
    $facebook = new Facebook(array(
            'appId' => $appId,
            'secret' => $appSecret,
            'cookie' => true
    ));     
    $accessToken = $facebook->getAccessToken();     
    $mdl = new Core_Model_Resource_Users();
    $user = $facebook->getUser();       
    if($user){
        try {
            $profile = $facebook->api('/me');               
            $newUserId = $mdl->insert(array(
                    'fuid' => $user,
                    'facebook_access_token' => $accessToken,
                    'first_name' => $profile['first_name'],
                    'middle_name' => $profile['middle_name'],
                    'last_name' => $profile['last_name'],
                    'status' => 'active',
                    'verified' => 1,
                    'hash' => Ranger_Application_Util::generateHash(),
                    'acl_role' => 'client',
                    'avatar' => 'default.png',
                    'date_created' => new Zend_Db_Expr("NOW()"),
                    'date_updated' => new Zend_Db_Expr("NOW()"),
                    'username' => 'user'.Ranger_Application_Util::generateHash()
            ));
        } catch(Exception $e){
            echo "Issue getting user information. ".$e->getMessage();
            die();
        }
    } else {
        echo "Issue getting user information";
        die();
    }

Now here is my zend plugin that detects facebook for usage of automatic sign in. This should only activate after the callback.

    public function preDispatch(Zend_Controller_Request_Abstract $request) {
        $appId = Ranger_Application_Util::getConfig('facebook_app_id');
        $appSecret = Ranger_Application_Util::getConfig('facebook_app_secret');
        require_once 'facebook/src/facebook.php';
        $facebook = new Facebook(array(
               'appId' => $appId,
               'secret' => $appSecret,
               'cookie' => true
        ));
        $user = $facebook->getUser();
        if($user){
            $fetchMode = Core::db()->getFetchMode();
            Core::db()->setFetchMode(Zend_Db::FETCH_OBJ);
            $userProfile = $facebook->api('/me');
            $mdl = new Core_Model_Resource_Users();
            $sel = $mdl->select();
            $sel->where('fuid=:fuid')->bind(array(":fuid"=>$user));
(OR:)       $sel->where('fuid=:fuid')->bind(array(":fuid"=>$userProfile['id']));
            $data = $mdl->fetchRow($sel);
            Core::db()->setFetchMode($fetchMode);
            Ranger_Application_Util::logUserIn($data->id);
      }

No matter how many times I do the following:

  • clean out cookies and history
  • use the $facebook->destroySession() method

Here is the MySQL Information:

FUID -> INT(30)
FACEBOOK_ACCESS_TOKEN -> VARCHAR(255)

Its still supplying me with two different IDs from $user on the detect portion of code Furthermore, it looks like the IDs are only inequal when using the detect code.

Any ideas what I should be doing?


Solution

  • you should not be using 2 different ways to get your user id stick with one and the Graph API is the recommended one. $p = $facebook->api("/me"); $p['id']

    Redirect code:

    $this->view->layout ()->disableLayout ();
        $this->_helper->viewRenderer->setNoRender ( true );
        $appId = Ranger_Application_Util::getConfig('facebook_app_id');
        $appSecret = Ranger_Application_Util::getConfig('facebook_app_secret');
        require_once 'facebook/src/facebook.php';
        $facebook = new Facebook(array(
                'appId' => $appId,
                'secret' => $appSecret,
                'cookie' => true
        ));     
        $accessToken = $facebook->getAccessToken();     
        $mdl = new Core_Model_Resource_Users();
        $user = $facebook->getUser();       
        if($user){
            try {
                $profile = $facebook->api('/me');               
                $newUserId = $mdl->insert(array(
                        'fuid' => $profile["id"],
                        'facebook_access_token' => $accessToken,
                        'first_name' => $profile['first_name'],
                        'middle_name' => $profile['middle_name'],
                        'last_name' => $profile['last_name'],
                        'status' => 'active',
                        'verified' => 1,
                        'hash' => Ranger_Application_Util::generateHash(),
                        'acl_role' => 'client',
                        'avatar' => 'default.png',
                        'date_created' => new Zend_Db_Expr("NOW()"),
                        'date_updated' => new Zend_Db_Expr("NOW()"),
                        'username' => 'user'.Ranger_Application_Util::generateHash()
                ));
            } catch(Exception $e){
                echo "Issue getting user information. ".$e->getMessage();
                die();
            }
        } else {
            echo "Issue getting user information";
            die();
        }
    

    again make sure that fuid column is set to a string type MySQL varchar(200) is what i use for facebook UIDs

    public function preDispatch(Zend_Controller_Request_Abstract $request) {
            $appId = Ranger_Application_Util::getConfig('facebook_app_id');
            $appSecret = Ranger_Application_Util::getConfig('facebook_app_secret');
            require_once 'facebook/src/facebook.php';
            $facebook = new Facebook(array(
                   'appId' => $appId,
                   'secret' => $appSecret,
                   'cookie' => true
            ));
            $user = $facebook->getUser();
            if($user){
                $fetchMode = Core::db()->getFetchMode();
                Core::db()->setFetchMode(Zend_Db::FETCH_OBJ);
                $userProfile = $facebook->api('/me');
                $mdl = new Core_Model_Resource_Users();
                $sel = $mdl->select();
                $sel->where('fuid='.$userProfile['id']);
                // i don't like bind for one variable php will be quicker  using string concatenation
                $data = $mdl->fetchRow($sel);
                Core::db()->setFetchMode($fetchMode);
                Ranger_Application_Util::logUserIn($data->id);
          }
    

    Thats all you should be doing