http://apps.facebook.com/karmabalance/
我正在尝试在我的 FB 应用程序上进行登录和授权,但它似乎做了一些有趣的事情。基本上我想要的是当用户点击我的应用程序时,我希望 fb 进行检查以确保他们已登录并已批准该应用程序,然后我需要程序检查数据库以查看是否他们已经是数据库中具有用户 FB ID 的记录。如果他们没有记录,我希望程序将用户发送到创建帐户。允许用户输入用户名的页面。如果有记录,我想将用户发送到应用主页。
我以为我让它工作了,但它仍然不正确,我不知道为什么。这是我的代码。我相信还有更好的方法可以做到这一点,我是新手,所以我正在努力解决这一切。任何帮助,将不胜感激。
谢谢
当用户点击我的应用程序时,它会转到这个 index.php 页面。
<?php
require 'config.php';
session_start();
$usercheck = $user_profile['id'];
$result = mysql_query("SELECT FBID FROM PLAYER WHERE (FBID = '$usercheck') ");
if ($facebook->getUser())
{
if(mysql_num_rows($result))
{
header('location: Home.php');
} else
{ ?>
<p>Please choose a Username for yourself. </p>
<form action='includes/signup.php' method='post'>
<fieldset style="width:600px";>
<label>Username</label>
<input name='username' type='text' value='<? if ($facebook->getUser())
{echo'Choose a Username';} ?>' />
<input name='submit' type='submit' value='submit' />
</fieldset>
</form>
<? }
} else
{ ?>
<p>Sign up with Facebook <fb:login-button perms='email'> Connect</fb:login-button>
It only takes a few seconds</p>
<div id='fb-root'></div>
<script src='http://connect.facebook.net/en_US/all.js'></script>
<script>
FB.init({
appId:'334230339967350', cookie:true,
status:true, xfbml:true
});
FB.Event.subscribe('auth.login', function(response) {
window.location.reload(); //will reload your page you are on
});
</script>
<? }
?>
配置文件:
<?php
//Facebook Configuration
require 'facebook-php-sdk/src/facebook.php';
$app_id = "xxxxxxxxxxxx";
$app_secret = "xxxxxxx";
$facebook = new Facebook(array(
'appId' => $app_id,
'secret' => $app_secret,
'cookie' => true
));
// Get User ID
$user = $facebook->getUser();
if ($user) {
try {
// Proceed knowing you have a logged in user who's authenticated.
$user_profile = $facebook->api('/me');
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}
// login or logout url will be needed depending on current user state.
if ($user) {
$logoutUrl = $facebook->getLogoutUrl();
} else {
$loginUrl = $facebook->getLoginUrl();
}
//SQL Configuration
// i start most if not all pages with this depending on what Im using it for.
$host = 'localhost'; // host name OR IP
$username = 'security';//username
$pass = 'blockedout'; //password
$dbname = 'Security'; // Database Name
$conn = mysql_connect($host, $username, $pass) or die(mysql_error());
if ($conn)
{
mysql_select_db($dbname) or die(mysql_error());
}
else
{
echo 'Connection failed.';
} // The above code connects to your database or errors if it cannot connect.
// Again this is simple, security is your own priority.
//GLOBAL VALUES??
$usercheck = $user_profile['id'];
$usernamequery = mysql_query("SELECT UserName FROM PLAYER WHERE (FBID = '$usercheck') ");
$username = mysql_fetch_array($usernamequery);
$levelquery = mysql_query("SELECT LevelID FROM PLAYER WHERE (FBID = '$usercheck') ");
$level = mysql_fetch_array($levelquery);
$result = mysql_query("SELECT FBID FROM PLAYER WHERE (FBID = '$usercheck') ");
?>
这是signup.php页面
<?
require_once '../config.php';
//here you could add checks for any empty fields using (!($_POST['first_name']))
$first_name = $user_profile['first_name']; // this line will collect our information from the
// field in our form that has the facebook first_name in it.
$last_name = $user_profile['last_name']; // same as above
$email = $user_profile['email']; //same as above
$id = $user_profile['id']; //same as above
$username1 = $_POST['username'];
$query = mysql_query
("
INSERT INTO PLAYER (FirstName, LastName, EMail, FBID, UserName, Status, LevelID, Cash, LifePoints, RespectPoints, ReputationPoints, UpgradePoints, HealthPercent)
VALUES ('$first_name', '$last_name', '$email', '$id', '$username1', '1', '1', '25000', '3', '3', '3', '20', '100')
")
or die(mysql_error());
// The query will insert our fields in to the database as the above line shows, make
//sure your database table headers are exactly correct otherwise this will not work
// You can now either send an email or if you wanted header to a new page. This is
//up to you. Tutorials on google will show you how to do this part
if($query){
header('location: ../Home.php');
}else {
echo 'error adding to database';
}
?>