Jump to content

Some Issue With Php Code I Assume


Guest FirefoxRocks

Recommended Posts

Guest FirefoxRocks

Goal: To allow people to sign up for my site exclusively using Facebook Connect.Conditions:

  • User not logged into Facebook, has not authorized my site -> Log in to Facebook, authorize the site, register on my site
  • User not logged into Facebook, site is authorized, has not signed up -> Log in to Facebook, register on my site
  • User not logged into Facebook, site is authorized, has signed up -> Log in to Facebook, should be logged in to my site automatically
  • User logged into Facebook, site is not authorized -> Authorize the site, register on my site
  • User logged into Facebook, site is authorized, has not signed up -> Log in to Facebook, register on my site
  • User logged into Facebook, site is authorized, has signed up -> Should be automatically logged in

Problems:

  • After logging out of Facebook, somehow the user ID is still accessible via the program, however name and photo is not visible.
  • After logging into Facebook, the name and photo still do not show up.
  • Whenever it works, sometimes the button says "Connect with Facebook" (with user photo) when the user is logged in, and it sometimes says "Facebook logout" (no user photo) when the user is logged out.

Bottom line: The user's name and photo should be visible when they are logged in. The button should say "Facebook logout" when they are logged in to Facebook. (And vice versa when the user is logged out.)Here is my PHP code for a few pages:index.php

<?php	header("Content-Type: text/html;charset=utf-8");		require_once 'facebook.php';	$appapikey = '06fc7a0e5f53e361d7fc4a3bf3638f7e';	$facebook = new Facebook($appapikey, 'SECRET_KEY_HERE');	(int)$user_id = $facebook->get_loggedin_user();?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml" xmlns:fb="http://www.facebook.com/2008/fbml"><head>	<title>TEST</title>	<script src="http://static.ak.connect.facebook.com/js/api_lib/v0.4/FeatureLoader.js.php" type="text/javascript"></script>	<link rel="stylesheet" type="text/css" href="styles.css" /></head><body onload="initFB();">	<div id="header">...</div>		<div id="body">		<div id="content">...</div>				<div id="sidebar">					<?php				if($_SESSION["authorized"]=="1") {							echo '<p><fb:profile-pic uid="loggedinuser" size="thumb" facebook-logo="true" /></p>';					echo "<p>Hi, <fb:name uid='$user_id' useyou='false' />!</p>";					echo '<p><fb:login-button v="2" autologoutlink="true" length="long"></fb:login-button></p>';				}				else {					echo '<p>Hi, Guest!</p><p><a href="login.php">Login!</a></p>';				}			?>		</div>	</div>			<script type="text/javascript">	function initFB() {		FB_RequireFeatures(["XFBML"], function(){FB.init("<?php echo $appapikey; ?>", "_receiver.html");});	}	</script>

login.php

<?php	header("Content-Type: text/html;charset=utf-8");		require_once 'facebook.php';	$appapikey = '06fc7a0e5f53e361d7fc4a3bf3638f7e';	$appsecret = '...';	$facebook = new Facebook($appapikey, $appsecret);	$user_id = $facebook->require_login();		if(!empty($user_id)) {		$dbconn = mysql_connect("localhost", "DB_USERNAME", "DB_PASSWORD");		if(!$dbconn) {			die("Error: Could not connect to database: " . mysql_error());		}				if(!mysql_select_db("DB_NAME", $dbconn)) {			die("Error selecting database: " . mysql_error());		}				(string)$q="SELECT `id` FROM `users` WHERE `fb_userid`='$user_id';";				if(mysql_num_rows(mysql_query($q))===1) {			$_SESSION['authorized']="1";			header("Location: http://.../index.php");		}		else {			header("Location: http://.../signup.php");		}	}	// else user is not logged in or not authorized?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml" xmlns:fb="http://www.facebook.com/2008/fbml"><head>	<title>...</title>	<script src="http://static.ak.connect.facebook.com/js/api_lib/v0.4/FeatureLoader.js.php" type="text/javascript"></script>	<script type="text/javascript">	function initFB() {		FB.init("", "_receiver.html",{"reloadIfSessionStateChanged":true});	}	</script></head><body onload="initFB();">	<div id="header">...</div>		<div id="body">			<h2>Login to Facebook</h2>						<p><fb:login-button length="long" size="large" /></p>	</div>

signup.php

<?php	header("Content-Type: text/html;charset=utf-8");		require_once 'facebook.php';	$appapikey = '06fc7a0e5f53e361d7fc4a3bf3638f7e';	$appsecret = '...';	$facebook = new Facebook($appapikey, $appsecret);	$user_id = $facebook->require_login();		if(empty($user_id)) {		header("Location: http://.../login.php");	}	else {		(array)$user_details=$facebook->api_client->users_getInfo($user_id, array('last_name','first_name'));	}?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head>	<title>...</title></head><body>	<div id="header">...</div>		<div id="body">			<h2>Signup</h2>			<p>We have retrieved ... from your Facebook profile. Please enter your email address to continue:</p>						<form action="signup2.php" method="post">						<p>Name: <?php echo "{$user_details[0]['first_name']} {$user_details[0]['last_name']}"; ?></p>			<p><label for="email">Email address:</label> <input type="text" maxlength="64" size="64" name="email" id="email" /></p>						<div><button type="submit">Register</button></div>			</form>	</div>

signup2.php

<?php		require_once 'facebook.php';	(string)$appapikey = '06fc7a0e5f53e361d7fc4a3bf3638f7e';	(string)$appsecret = '...';	$facebook = new Facebook($appapikey, $appsecret);	$user_id = $facebook->require_login();		if(empty($user_id)) {		$error = "There was a problem with logging into Facebook. Please log out of Facebook and log in again.";	}	elseif(!filter_input(INPUT_POST, "email", FILTER_VALIDATE_EMAIL)) {		$error = "\"{$_POST["email"]}\" is not a valid email address.";	}	else {		(array)$user_details=$facebook->api_client->users_getInfo($user_id, array('last_name','first_name'));		(string)$email = $_POST["email"];			$dbconn = mysql_connect("localhost", "DB_USERNAME", "DB_PASSWORD");		if(!$dbconn) {			die("Error: Could not connect to database: " . mysql_error());		}				if(!mysql_select_db("DB_NAME", $dbconn)) {			die("Error selecting database: " . mysql_error());		}		else {			$r = mysql_query("INSERT INTO `users` (`fb_userid`, `name`, `email`) VALUES ('$user_id', '{$user_details[0]['first_name']} {$user_details[0]['last_name']}', '$email');");						if(!$r) {$error = "Error adding user: ".mysql_error();}			else {				// do something if successful			}		}	}	// else user is not logged in or not authorized	header("Content-Type: text/html;charset=utf-8");		// the error is printed out below somewhere if there is one?>

Why is this not working?Relevant pages:http://developers.facebook.com/connect.phphttp://wiki.developers.facebook.com/index....d_Authorizationhttp://wiki.developers.facebook.com/index....zation_WebsitesAnd a few more...but you can find them if you need to from links on those pages

Link to comment
Share on other sites

you wont get a responce on this forum not many people on this forum would use facebook connect.there is a forum on facebook for help like this im sure you can see this on your application help/setup page!

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...