gpt4 book ai didi

php - 删除多页重新加载问题

转载 作者:行者123 更新时间:2023-11-30 17:56:41 25 4
gpt4 key购买 nike

我正在尝试获取用户最喜欢的电影和其他详细信息以及 FB JS SDK。以下对我来说是正确的。我曾尝试使用 ajax 来删除页面重新加载。但我不明白为什么在登录按钮出现之前页面会重新加载两次。并且在用户输入登录凭据并单击登录按钮后,在数据出现之前页面会重新加载两次。我想删除这个多页重新加载。有人可以检查代码并使它更容易吗?

登录.php:

<?php
session_start();
$appId = '66';
$appSecret = 'sdsds3'; // Facebook App Secret
$return_url = 'http://yoursite.com/connect_script/'; //path to script folder
$fbPermissions = 'publish_stream,email'; //more permissions : https://developers.facebook.com/docs/authentication/permissions/

?>
<!DOCTYPE html>
<html xmlns:fb="http://www.facebook.com/2008/fbml" xml:lang="en-gb" lang="en-gb" ><head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<title>JS/Ajax Facebook Connect</title>
<script>
function AjaxResponse()
{
var myData = 'connect=1';
$.ajax({
type: "POST",
url: "process_facebook.php",
data: myData
}).done(function(result) {
$("#fb-root").html(result);
});
}

function LodingAnimate() //Show loading Image
{
$("#LoginButton").hide(); //hide login button once user authorize the application
}
</script></head><body>
<?php
if(!isset($_SESSION['logged_in']))
{
?>
<div id="results"> </div>
<div id="LoginButton">
<div class="fb-login-button" onlogin="javascript:CallAfterLogin();" size="medium" scope="<?php echo $fbPermissions; ?>">Connect With Facebook</div>
</div>
<?php
}
?>
<div id="fb-root"></div>
<script type="text/javascript">
window.fbAsyncInit = function() {
FB.init({
appId: '<?php echo $appId; ?>',
cookie: true,
xfbml: true,
channelUrl: '<?php echo $return_url; ?>channel.php',
oauth: true});};

(function() {
var e = document.createElement('script');
e.async = true;
e.src = document.location.protocol +'//connect.facebook.net/en_US/all.js';
document.getElementById('fb-root').appendChild(e);}());

function CallAfterLogin(){
FB.login(function(response) {
if (response.status === "connected")
{
LodingAnimate(); //Animate login
FB.api('/me', function(data) {
// $("#fb-root").append(data.);
console.log(data);
if(data.email == null)
{
//Facbeook user email is empty, you can check something like this.
alert("You must allow us to access your email id!");
ResetAnimate();

}else{
AjaxResponse();
} });
} }); }
</script>
</body>
</html>

process_facebook.php:

<?php
session_start();
$appId = '66549'; //Facebook App ID
$appSecret = '3944'; // Facebook App Secret
$return_url = 'http://yoursite.com/connect_script/'; //path to script folder
//$fbPermissions = 'publish_stream,email'; //more permissions : https://developers.facebook.com/docs/authentication/permissions/

if(isset($_POST["connect"]) && $_POST["connect"]==1)
{
if (!class_exists('FacebookApiException')) {
require_once('facebook.php' );
}
$facebook = new Facebook(array(
'appId' => $appId,
'secret' => $appSecret,
));
$fbuser = $facebook->getUser();
if ($fbuser) {
try {
$user_interest = $facebook->api('/me/movies');
//print user facebook data
echo '<pre>';
//print_r($user_interest);
foreach ($user_interest['data'] as $var)
{
$ID=$var['id'];
$Name=$var['name'];
echo '<br/>';
print_r($Name); echo '<br/>';
print_r($ID);

}
echo '</pre>';
}
catch (FacebookApiException $e) {
echo '<pre>'.htmlspecialchars(print_r($e, true)).'</pre>';
$user = null;
}}}
?>

最佳答案

代替具有“onlogin”和“scope”属性的“fb-login-button”,只需制作一个简单的按钮/链接/任何东西并在“onclick”事件中调用“CallAfterLogin”。例如,您没有正确使用 FB.login,那是您必须放入范围的地方。

参见此处:https://developers.facebook.com/docs/reference/javascript/FB.login/

顺便说一句,我不会调用函数“CallAfterLogin”,因为它不是在登录后调用的,它是登录代码。而且您根本不需要 process_facebook.php,您只需使用 JavaScript SDK 即可,无需使用 jQuery/Ajax。

关于php - 删除多页重新加载问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17938488/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com