我有一个网站,您可以通过Facebook登录。我登录我的网站,一切都很好。但是在访问www.facebook.com并注销后,当我回到网站上并进行刷新时,该应用程序的工作方式就像我登录Facebook一样。我尝试了各种各样的条件,例如:
$user_profile = $facebook->api('/me');
if($user_profile !== $_SESSION['id']) {
header( 'Location: logout.php' ) ;
}
但没有任何作用,如果用户未登录Facebook,如何将其发送到退出页面?
这是我的代码:
<?php
// Application Configurations
$app_id = "xxxxxxxxxxxxxxxx";
$app_secret = "xxxxxxxxxxxxxxx";
$site_url = "xxxxxxxxxxxxxxx";
try {
include_once "src/facebook.php";
}
catch(Exception $e){
error_log($e);
}
// Create our application instance
$facebook = new Facebook(array(
'appId' => $app_id,
'secret' => $app_secret,
'cookie' => true,
));
// Get User ID
$user = $facebook->getUser();
$user_profile = null;
// Get the current access token
// We may or may not have this data based
// on whether the user is logged in.
// If we have a $user id here, it means we know
// the user is logged into
// Facebook, but we don’t know if the access token is valid. An access
// token is invalid if the user logged out of Facebook.
// print_r($user);
if ($session) {
echo "este";
} else {
echo "nu este";
}
if(!isset($_SESSION['id']))
{
if($user){
// Proceed knowing you have a logged in user who's authenticated.
$user_profile = $facebook->api('/me');
// Get logout URL
$logoutUrl = $facebook->getLogoutUrl();
try{
//Connecting to the database. You would need to make the required changes in the common.php file
//In the common.php file you would need to add your Hostname, username, password and database name!
mysqlc();
$id = $user_profile['id'];
$name = $user_profile['name'];
$email = $user_profile['email'];
$gender = $user_profile['gender'];
$bio = $user_profile['bio'];
$query = sprintf("SELECT * FROM newmember WHERE id = %s", $id);
$results = mysql_query($query) or die('tubRandom - Query failed: ' . mysql_error() . "<br />\n$sql");
if(mysql_num_rows($results) == 0)
{
$query_two = sprintf("INSERT INTO newmember values(%s,%s,%s,%s,%s,'yes')", $id, $name, $email, $gender, $bio);
$insert_query_two = mysql_query($query_two) or die('tubRandom - Query failed: ' . mysql_error() . "<br />\n$sql");
$_SESSION['id'] = $user_profile['id'];
} else {
$rows = mysql_fetch_array($results);
$_SESSION['id'] = $user_profile['id'];
}
}
catch(FacebookApiException $e){
error_log($e);
$user = NULL;
}
} else {
// Get login URL
$loginUrl = $facebook->getLoginUrl(array(
'scope' => 'read_stream, publish_stream, email, user_about_me, user_website, user_work_history, user_relationships, user_religion_politics, user_relationships, user_likes, user_location, user_relationship_details, user_hometown, user_education_history',
'redirect_uri' => $site_url,
));
}
} else {
$user_profile = $facebook->api('/me');
echo $user_profile['name'] . $user_profile . $user_profile['id'] . $_SESSION['id'];
//header( 'Location: logout.php' ) ;
}
?>
对不起,如果这是一个虚拟问题,我是一个女孩。 :d
答案 0 :(得分:0)
嗯,首先我不明白为什么你要记下你的性别。无论如何,请查看FB.getLoginStatus函数。
答案 1 :(得分:0)
您可以通过 $ facebook-&gt; getUser(); 查看当前用户的Facebook用户ID,如果没有登录用户,则返回0;) ;):P