Login Fb Sdk

Hello guys, I have problem with logind by fb because getUser() return 0 and script not working correctly.

My method looks like:




    public function actionLoginFb() {


	Yii::import('ext.facebook', true);


	$facebook = new Facebook(array(

		    'appId' => 'xxx',

		    'secret' => 'xxx',

		    'cookie' => true

		));


	$user_id = $facebook->getUser();


	if ($user_id) {

	    $fb_data = $facebook->api('/me');


	    if (!$fb_data['email'] || $fb_data['email'] == '') {

		Yii::app()->user->setFlash('error', "Twoje konto FB nie udostępnia aplikacjom Twojego adresu e-mail. Zmień ustawienia udostępniania lub zarejestruj się uzupełniając dane w formularzu rejestracyjnym.");

		$this->redirect('rejestracja/loginfb');

	    }

	    

	    

	} else {

	    $log_in_url = $facebook->getLoginUrl(array(

	    'redirect_uri' => 'http://localhost/yii/blog/rejestracja/loginfb',

	    'scope' => array("email")

	    ));

	    }

	    

	    

	    

	    $this->render('/login/login_fb', array(

		'user' => $user_id,

		'loginUrl' => $log_in_url

	    ));


	

}



what I m doing wrong? any ideas?

I found this code in google and its working for me:




$app_id = "xxxx";

	$app_secret = "xxxx";

	$facebook = new Facebook(array(

		    'appId' => $app_id,

		    'secret' => $app_secret,

		    'cookie' => true

		));


	$user = $facebook->getUser();

	

	if ($user <> '0' && $user <> '') { /* if valid user id i.e. neither 0 nor blank nor null */

	    try {

// Proceed knowing you have a logged in user who's authenticated.

		$user_profile = $facebook->api('/me');

	    } catch (FacebookApiException $e) { /* sometimes it shows user id even if user in not logged in and it results in Oauth exception. In this case we will set it back to 0. */

		error_log($e);

		$user = '0';

	    }

	}

	if ($user <> '0' && $user <> '') {

	    /* So now we will have a valid user id with a valid oauth access token and so the code will work fine. */

	    

	    

	    echo "UserId : " . $user;


	    $params = array('next' => 'http://localhost/yii/blog/');

	    echo "<p><a href='" . $facebook->getLogoutUrl($params) . "'>Logout</a>";


	    $user_profile = $facebook->api('/me');

	    echo "<p>Name : " . $user_profile['name'];

	    echo "<p>";

	    print_r($user_profile);

	    

	    

	    

	} else {/* If user id isn't present just redirect it to login url */

	    header("Location:{$facebook->getLoginUrl(array('req_perms' => 'email,offline_access'))}");

	}