Click here to Skip to main content
15,917,808 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
$facebook-> getuser() always returns 0
I checked many websites for solution but not get any solution

This is my code:
PHP
include_once "facebook-php-sdk-master/src/facebook.php";
//initializing
$config = array(
      'appId' => 'myappid',
      'secret' => 'myappsecret',
	  'cookie' => true
  );

  $facebook = new Facebook($config);
  //getting user id 
  $uid = $facebook->getUser();
  if($uid)
  	{
		echo $uid;
		$params = array( 'next' => $currentUrl );
		$logoutUrl=$facebook->getLogoutUrl($params); // $params is optional
		echo "<a target="_blank" href="$logoutUrl">Logout from FB</a>";
		//$facebook->destroySession();
		try
		{
			$user_profile = $facebook->api('/me','GET');
       		echo "Name: " . $user_profile['name'];
		}
		catch(FacebookApiException $e)
		{
			echo "ERROR";
		}
	 }
  else
  	{
		//if user is not logged in then get the login user
		$params = array(
			  'scope' => 'read_stream, friends_likes, email',
			  'redirect_uri' => 'http://www.comettea.com',
			  'display' => 'popup',
				);

		$loginUrl = $facebook->getLoginUrl($params);
		echo $loginUrl."<br />";
		echo "<a target="_blank" href=".$loginUrl."> Login in FB </a>";
	}


please help
Posted
Updated 17-Feb-14 6:31am
v2
Comments
Thomas Daniels 16-Feb-14 13:06pm    
Can you show your entire code please?
CHITRAKSH 2010 16-Feb-14 23:37pm    
include_once "facebook-php-sdk-master/src/facebook.php";
//initializing
$config = array(
'appId' => 'myappid',
'secret' => 'myappsecret',
'cookie' => true
);

$facebook = new Facebook($config);
//getting user id
$uid = $facebook->getUser();
if($uid)
{
echo $uid;
$params = array( 'next' => $currentUrl );
$logoutUrl=$facebook->getLogoutUrl($params); // $params is optional
echo "Logout from FB";
//$facebook->destroySession();
try
{
$user_profile = $facebook->api('/me','GET');
echo "Name: " . $user_profile['name'];
}
catch(FacebookApiException $e)
{
echo "ERROR";
}
}
else
{
//if user is not logged in then get the login user
$params = array(
'scope' => 'read_stream, friends_likes, email',
'redirect_uri' => 'http://www.comettea.com',
'display' => 'popup',
);

$loginUrl = $facebook->getLoginUrl($params);
echo $loginUrl."<br />";
echo " Login in FB ";
}

1 solution

Try and read the facebook developer or login API.
try this
https://developers.facebook.com/docs/php/gettingstarted/[^]
 
Share this answer
 
Comments
CHITRAKSH 2010 16-Feb-14 23:34pm    
Thank you Sir for response.
I have read the same and tried but every time $facebook-> getuser() returns 0.
It return with current URL + a "code" parameter
the value of code is

AQApCZ3JHKYX_FdSwcWSrvk-IF4rxVmiNsu3YeI89kn64YCmutLspYKt1iusywBqwJZrKA5ZnkTDo0Jc0TzP_dzjxuUcCmSfNfPvwoKEiJfIG0oEuWs4wILYhaeJuVpO1RYyz7V0A0-3iI0oO6tcxoCKxBhcZ6NIAk8l_0U8W5IQ3P9qJmuC-3Md4N65z4TQeKZ7nqPTOo1zJTtrG_YZ_5XLqpRlcnQm_6vzje57OJX9heUP_UUHs9oqKz7Czta2EEtzpoVktn7DS8TBZOwRZ1CAIS_KyHYy8QGZj_NKH4JllGmd8D3226njjYaTGpbNDyU&state=f03af5b926770594e7522ef619f6b479#_=_

Please guide/help
ahmedfaruk88 17-Feb-14 1:19am    
Do you have an access token and please can you post your code...
CHITRAKSH 2010 17-Feb-14 3:09am    
Sir
Please find below my code

include_once "facebook-php-sdk-master/src/facebook.php";
//initializing
$config = array(
'appId' => 'myappid',
'secret' => 'myaddsecret',
'cookie' => true
);

$facebook = new Facebook($config);
//getting user id
$uid = $facebook->getUser();
if($uid)
{
echo $uid;
$params = array( 'next' => $currentUrl );
$logoutUrl=$facebook->getLogoutUrl($params); // $params is optional
echo "Logout from FB";
//$facebook->destroySession();
try
{
$user_profile = $facebook->api('/me','GET');
echo "Name: " . $user_profile['name'];
}
catch(FacebookApiException $e)
{
echo "ERROR";
}
}
else
{
//if user is not logged in then get the login user
$params = array(
'scope' => 'read_stream, friends_likes, email',
'redirect_uri' => 'http://www.comettea.com',
'display' => 'popup',
);

$loginUrl = $facebook->getLoginUrl($params);
echo $loginUrl."<br />";
echo " Login in FB ";
}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900