早上好,在Facebook登录后,我终于设法将Facebook用户名存储在我的数据库中。唯一的问题是需要重新加载Facebook登录后用户重定向到的页面(我第一次只得到一个空白页面)。你能参考我之前的问题吗,因为我已经在那里发布了所有代码
更新:我注意到页面只需要在第一次刷新(当用户信息尚未存储在数据库中时,它会快速而很好地加载页面。请帮忙!;-)
UPDATE2:有没有办法在添加新用户后自动刷新页面(仅一次)?非常感谢!
更新3:我发布代码。它仅在我刷新页面时有效。知道吗?
<?php
mysql_connect('host', 'username', 'password');
mysql_select_db('table');
require 'library/facebook.php';
// Create our Application instance
$facebook = new Facebook(array(
'appId' => 'MYID',
'secret' => 'MYSECRET',
'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;
}
}else{
header('location: index.php');
}
$query = mysql_query("SELECT * FROM users WHERE oauth_provider = 'facebook' AND oauth_uid = ". $user_profile['id']);
$result = mysql_fetch_array($query);
if(empty($result)){
$query = mysql_query("INSERT INTO users (oauth_provider, oauth_uid, username) VALUES ('facebook', {$user_profile['id']}, '{$user_profile['name']}')");
$query = msyql_query("SELECT * FROM users WHERE id = " . mysql_insert_id());
$result = mysql_fetch_array($query);
}
if ($user) {
$paramsout = array('next'=>'http://www.mywebsite/test/logout.php');
$logoutUrl = $facebook->getLogoutUrl($paramsout);
}
?>
更新 4*****:我找到了解决方案。你可以在下面看看我的回答。谢谢大家。
当Facebook使用下一个或重定向URL重定向时,它会发送API获取的帖子数据,但是当您手动重定向页面时,它不会获取Facebook帖子数据,因此else condition header('location: index.php');
中的代码被执行并且您被正确重定向。
第一次的问题是因为它通过传递 else 条件并转到 SQL 插入新记录,之后没有重定向......所以你需要最后放header('location: index.php');
,以便在数据库中插入用户记录后重定向它
希望这有帮助
使用
header("Location: url");
exit();
完成所有必要的操作后,假设您此时不发送任何标头。否则,您可以在生成的文档中使用 META REFRESH 或 JS document.location.href。
在脚本末尾添加header("Location: $url");
。
我认为您必须更新脚本以尝试加载或获取$user配置文件,而不是仅加载用户..也许它有效
require 'lib/facebook-php-sdk/src/facebook.php';
define('APP_TITLE', 'Cool project');
define('APP_DESCRIPTION', 'Very nice project');
define('APP_ID', '168565339939940');
define('APP_SECRET', '4127eee9d3ab29a114e80h89060243a7');
define('APP_URL', 'https://localhost/project/');
define('APP_PERMISSIONS', '');
$user_id = null;
$user_profile = null;
$fb = new Facebook(array(
'appId' => APP_ID,
'secret' => APP_SECRET,
'cookie' => true,
));
$loginUrlParams = array(
'scope' => APP_PERMISSIONS,
'redirect_uri' => APP_URL,
);
$user_id = $fb->getUser();
if ( $user_id )
{
try
{
$data = $fb->api('/me');
$user_profile = array(
'user_id' => $user_id,
'name' => isset($data['name']) ? he($data['name']) : null,
'avatar' => 'https://graph.facebook.com/'.$user_id.'/picture',
);
unset($data);
}
catch ( FacebookApiException $e )
{
header('Location: ' . $fb->getLoginUrl($loginUrlParams));
exit;
}
}
else
{
header('Location: ' . $fb->getLoginUrl($loginUrlParams));
exit;
}
最后我做到了。当然,这是一个重定向问题。但是它既没有在索引中添加 sql.php也没有在 user_page.php 中添加。我最终将"login.php"装箱,其中用户从Facebook页面重定向,如果是新记录,则添加到数据库中,然后重定向到user_page.php。这是登录代码.php:
<?php
mysql_connect('HOST', 'USER', 'PSW');
mysql_select_db('MYDATABASE');
require 'library/facebook.php';
// Create our Application instance (replace this with your appId and secret).
$facebook = new Facebook(array(
'appId' => 'MYID',
'secret' => 'MYSECRET',
'cookie' => true));
// Get User ID
$user = $facebook->getUser();
if($user){
$user_profile = $facebook->api('/me');
$query = mysql_query("SELECT * FROM users WHERE oauth_provider = 'facebook' AND oauth_uid = ". $user_profile['id']);
$result = mysql_fetch_array($query);
if(empty($result)){
$query = mysql_query("INSERT INTO users (oauth_provider, oauth_uid, username) VALUES ('facebook', {$user_profile['id']}, '{$user_profile['name']}')");
header('Location: user_page.php');
}else{
header('Location: user_page.php');
}
}else{
header('Location: logout.php');
}
?>
或者使用 javascript 或 jquery,它们会在运行时告诉您不必重定向