Jump to content
Larry Ullman's Book Forums

Facebook Connect Doesnt Work


nkhanna24
 Share

Recommended Posts

public function actionFblogin()
    {
        $facebook = new Facebook(Yii::app()->params['fb']['config']);
        //$session = $facebook->getSession();
	$user = $facebook->getUser();
        $me = null;

        // Session based API call.
        if ($user)
        {
            $fb_uid = $facebook->getUser();

            if (Yii::app()->user->id)
            {
                $user = User::model()->find('fb_uid=?', array($fb_uid));
                if ($user === NULL)
                {
                    Yii::app()->user->setState('fb_uid', $fb_uid);
                    $this->redirect($this->createAbsoluteUrl('profile/settings', array('fbconnect' => 1)));
                    Yii::app()->end();
                }
                else
                {
                    $this->redirect($this->createAbsoluteUrl('profile/settings', array('fbconnect' => 2)));
                    Yii::app()->end();
                }
            }
            else
            {
            	$user = User::model()->find('fb_uid=?', array($fb_uid));
            	if($user->lastLogin==0)
            		$firstTime=1;
                $_identity = new FbUserIdentity($fb_uid);
                $_identity->authenticate();

                if ($_identity->errorCode === FbUserIdentity::ERROR_NONE)
                {
                    $duration = 3600 * 24 * 30;   // $this->rememberMe ? 3600*24*30 : 0; // 30 days
                    Yii::app()->user->login($_identity, $duration);
                    if($firstTime==1)
                    	$this->redirect($this->createAbsoluteUrl('site/demo', array('name' => $user->firstName)));
                    else
                    	$this->redirect(Yii::app()->user->returnUrl);
                    return true;
                }
                else
                    $this->redirect($this->createAbsoluteUrl('site/fbregister'));
            }
        }
        else
        {
            $this->redirect($facebook->getLoginUrl(array('req_perms' => Yii::app()->params['fb']['config']['perms'], 'return_url' => $this->createAbsoluteUrl('site/fblogin'))));
        }
    }

 

hi 

i have this actionFblogin in my sitecontroller.php

the problem is when i click the button an error occur and it doesnt redirect to my website ?

can anybody help me???

Link to comment
Share on other sites

Link to comment
Share on other sites

I've looked at the Facebook PHP API a bit. Anyone can find it here if interested: https://developers.facebook.com/docs/reference/php/

 

A general problem is that you don't read that API. I doubt that's really the issue, but take getLoginUrl() as an example:

 

From the API:

 

getLoginUrl(): Get a URL that the user can click to login, authorize the app, and get redirected back to the app.
Example:
 
$params = array(
   'scope' => 'read_stream, friends_likes',
   'redirect_uri' => 'https://www.myapp.com/post_login_page'
);

$loginUrl = $facebook->getLoginUrl($params);
 
You don't give facebook any of the keys they require. No scope and no redirect_uri.
 
Facebook provides some application code example here btw:
- https://github.com/facebook/facebook-php-sdk/blob/master/examples/example.php
 
Your main problem is that you redirect way to much. I don't really think you should do that at all considering the example usage above.
  • Upvote 1
Link to comment
Share on other sites

 Share

×
×
  • Create New...