Facebook Connect Script

Use this Facebook Connect script when you want to let your users to login or do stuff on your site without having to create an account.

<?
/* 
	For this to work you will have to download the Facebook PHP SDK
	https://github.com/facebook/php-sdk/
*/
require('facebook.php');
/*
	Set these variables to application's ones
*/
$app_id = 'application id here';
$app_secret = 'application secret here';
// init fb object
$facebook = new Facebook(array(
	'appId'		=> $app_id,
	'secret'	=> $app_secret,
	'cookie'	=> false
));
// get a session for user
$session = $facebook->getSession();
$me = null;
// if have session get user data
if ($session)
{
	try
	{
		$uid = $facebook->getUser();
		$me = $facebook->api('/me');
	}
	catch(FacebookApiException $e)
	{
		error_log($e);
	}
}
// if we have user data show logout button, else show login button with extended permission to get his email addy
$data = '';
if ($me)
{
	// store user data here!!!
	print_r($me);
	$logoutUrl = $facebook->getLogoutUrl();
	$data = '<a href="'.$logoutUrl.'"><img src="http://static.ak.fbcdn.net/rsrc.php/z2Y31/hash/cxrz4k7j.gif"></a>';
}
else
{
	$loginUrl = $facebook->getLoginUrl(array('req_perms' => 'email,read_stream'));
	$data = '<a href="'.$loginUrl.'"><img src="http://static.ak.fbcdn.net/rsrc.php/zB6N8/hash/4li2k73z.gif"></a>';
}
// for javascript side use the same session
$fb_session = json_encode($session);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Login with Facebook!</title>
</head>
<body>
	<p>Hello!</p>
    <div id="fb-root"></div>
    <script type="text/javascript" src="http://connect.facebook.net/en_US/all.js"></script>
    <script type="text/javascript">
    //<![CDATA[
    FB.init({
		appId: '<?=$app_id;?>', 
		cookie: true, 
		status: true, 
		xfbml: true, 
		session: <?=$fb_session;?>
	});
    FB.Event.subscribe('auth.login', function(){
		window.location.reload();
	});
    //]]>
    </script>
</body>
</html>

Usage

How to use it: Don't forget to download the PHP SDK. Test this script on a PHP page to see how it works and then try to integrate it in your pages. This Facebook Connect script will also request permission from the user to give to your website access to his e-mail address. You can see that in line 36 is where you get user's data. Code with Love.


Comments

guest
Posted on 18.12.2012 02:48

change $facebook->getSession() to $facebook->getUser()

guest
Posted on 31.08.2012 09:20

where is the file "facebook.php"?. I need it!!

guest
Posted on 13.07.2012 09:29

Thanks a lot!! After Log in.. I want to take user details.. U r code helped me.. :)

guest
Posted on 03.07.2012 11:27

I already change getSession() to getUser(). Why It doesn't show Facebook Connect button to login?

guest
Posted on 29.05.2012 18:13

where is the file "facebook.php"?. I need it!!

guest
Posted on 13.03.2012 06:48

this is outdated code.

steve
Posted on 03.03.2012 07:16

Actually this code is kinda old, needs to be updated to new FB API standards.

guest
Posted on 03.03.2012 07:12

getSession() is depreciated use getuser()

guest
Posted on 13.12.2011 11:22

hmmmmm

guest
Posted on 22.09.2011 11:10

Hi
It is giving following error:
Fatal error: Call to undefined method Facebook::getSession() in facebook-connect-script.php on line 19

guest
Posted on 15.08.2011 05:10

Well i want to get user information like User Name,User Email , Status , User Pic
etc kindly help me......

Add your comment