我已上传此代码以解决Facebook登录问题。有3个文件可以实现此功能.. (login.php/playground_fb.php/member_index_fb.php)
...首先,您可以找到href
到playground_fb.php
的简单按钮。然后在playground_fb.php
中有一些算法,然后最后如果用户接受这个应用程序,然后使用电子邮件地址,名字,姓氏等信息会话转到member_index_fb.php
...我搜索了很多关于这些问题,但我只找到了输入session_start
或停止会话或保持“www
”这样的建议......但它仍然保持不变......还有谁帮我请?
<!-- login.php -->
<div onclick="facebookLogin()" style="cursor: pointer;" align="center">
<img src="../build/img/login/login_facebook.png">
</div>
function facebookLogin() {
top.location.href = 'http://www.peeknchews.com/playground_fb.php';
}
<!-- playground_fb.php -->
<?php
session_start();
require_once('/home/kukkim/peeknchews.com/build/apis/facebook-php-sdk-v4/src/Facebook/autoload.php');
$fb = new Facebook\Facebook([
'app_id' => '257184091349723',
'app_secret' => '6c079349483a1bc959ce515df4e8138a',
'default_graph_version' => 'v2.8',
]);
$helper = $fb->getRedirectLoginHelper();
try {
if(isset($_SESSION['facebook_access_token'])) {
$accessToken = $_SESSION['facebook_access_token'];
}
else {
$accessToken = $helper->getAccessToken();
}
}
catch(Facebook\Exceptions\FacebookResponseException $e) {
//When Graph returns an error
echo 'Graph returned an error: ' . $e->getMessage();
exit;
}
catch(Facebook\Exceptions\FacebookSDKException $e) {
//When validation fails or other local issues
echo 'Facebook SDK returned an error: ' . $e->getMessage();
exit;
}
if(!isset($accessToken)) {
$permissions = ['email']; // optional
$loginUrl = $helper->getLoginUrl('http://'.$_SERVER['SERVER_NAME'].'/playground_fb.php', $permissions);
// die($_SESSION['FBRLH_' . 'state']);
echo "<script> top.location.href = '$loginUrl'; </script>";
// $permissions = ['email']; // optional
// $loginUrl = $helper->getLoginUrl('http://www.peeknchews.com/playground_fb.php', $permissions);
// echo '<a href="' . $loginUrl . '">LOG IN WITH FACEBOOK!</a>';
}
else {
if(isset($_SESSION['facebook_access_token'])) {
$fb->setDefaultAccessToken($_SESSION['facebook_access_token']);
}
else {
// Logged in!
$_SESSION['facebook_access_token'] = (string) $accessToken;
// OAuth 2.0 client handler
$oAuth2Client = $fb->getOAuth2Client();
// Exchanges a short lived access token for a long lived one
$longLivedAccessToken = $oAuth2Client->getLongLivedAccessToken($_SESSION['facebook_access_token']);
$fb->setDefaultAccessToken($_SESSION['facebook_access_token']);
}
try {
$response = $fb->get('/me?fields=email,first_name,last_name,name');
$userNode = $response->getGraphUser();
}
catch(Facebook\Exceptions\FacebookResponseException $e) {
// When Graph returns an error
echo 'Graph returne d an error: ' . $e->getMessage();
unset($_SESSION['facebook_access_token']);
exit;
}
catch(Facebook\Exceptions\FacebookSDKException $e) {
// When validation fails or other local issues
echo 'Facebook SDK returend an error: ' . $e->getMessage();
exit;
}
$fbId = $userNode->getId();
$fbEmail = $userNode->getProperty('email');
$fbName = $userNode->getName();
$fbFirstName = $userNode->getProperty('first_name');
$fbLastName = $userNode->getProperty('last_name');
$_SESSION['fbId'] = $fbId;
$_SESSION['fbEmail'] = $fbEmail;
$_SESSION['fbName'] = $fbName;
echo 'Name: ' . $fbName;
echo "<br>";
echo 'User ID: ' . $fbId . '<br>';
echo 'Email: ' . $fbEmail . '<br><br>';
echo 'Firstname : ' . $fbFirstName . '<br><br>';
echo 'Lastname : ' . $fbLastName . '<br><br>';
$image = 'https://graph.facebook.com/' . $fbId . '/picture?width=100';
echo "Picture<br><br>";
echo "<img src='$image' /><br><br> ";
//Now you can redirect to another page and use the
// access token from $_SESSION['facebook_access_token']
echo "<script>top.location.href = 'http://www.peeknchews.com/member_index_fb.php';</script>";
}
?>
<!-- member_index_fb.php -->
<?php
session_start();
require_once('/home/kukkim/peeknchews.com/build/apis/facebook-php-sdk-v4/src/Facebook/autoload.php');
$sessionEmail=$_SESSION['user_email'];
//facebook
$fbSessionId = $_SESSION['fbId'];
$fbSessionEmail = $_SESSION['fbEmail'];
$fbSessionName = $_SESSION['fbName'];
require_once('/home/kukkim/peeknchews.com/config.php');
?>
<li>
<a class="nav-login fancybox fancybox.iframe" href="php_login/login.php"><img src="build/img/icons/login.png">
<?php
echo "<br>";
echo $fbSessionName . "<br>";
echo $fbSessionEmail . "<br>";
$image = 'https://graph.facebook.com/' . $fbSessionId . '/picture?width=100';
echo "<img src='$image' /><br><br> ";
?>
</a>
</li>