您好,我尝试在用户单击“使用 facebook 登录”按钮时设置一个 POST。这是因为在我编写的代码中,如果您已注销,但在 facebook 中登录,那么您会自动再次登录该网站...所以如果你点击注销,你就永远不会注销..
为了避免这种情况,我尝试设置一个 POST ,设置一个经典条件
if ( isset ($_POST['fbconnect']))
我只需要添加一个 Conceal 输入并使用 inf fb:login-button 发送该表单
这是我尝试过的..但没有运气。
<form name="fbconnect" method="post">
<input type="hidden" name="fbconnect" value="fbconnect" />
<div id="fb-root"></div>
<fb:login-button scope='email,user_birthday'></fb:login-button></form>
<?php
}
?>
<script>
window.fbAsyncInit = function() {
FB.init({
appId : <?=YOUR_APP_ID?>,
status : true,
cookie : true,
xfbml : true,
oauth : true,
});
FB.Event.subscribe('auth.login', function(response) {
// ------------------------------------------------------
// This is the callback if everything is ok
$(this).parents('form').submit();
});
};
(function(d){
var js, id = 'facebook-jssdk'; if (d.getElementById(id)) {return;}
js = d.createElement('script'); js.id = id; js.async = true;
js.src = "//connect.facebook.net/en_US/all.js";
d.getElementsByTagName('head')[0].appendChild(js);
}(document));
</script>
我也尝试用表单名称替换 $(this).parents('form').submit();
form ,但它也不起作用
我也尝试过这个
<script>
window.fbAsyncInit = function() {
FB.init({
appId : <?=YOUR_APP_ID?>,
status : true,
cookie : true,
xfbml : true,
oauth : true,
});
FB.Event.subscribe('auth.login', function(response) {
// ------------------------------------------------------
// This is the callback if everything is ok
var url = "index.php";
var params = "fbconnect=fbconnect";
http.open("POST", url, true);
//Send the proper header information along with the request
http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
http.setRequestHeader("Content-length", params.length);
http.setRequestHeader("Connection", "close");
http.onreadystatechange = function() {//Call a function when the state changes.
if(http.readyState == 4 && http.status == 200) {
alert(http.responseText);
}
}
http.send(params);
});
};
(function(d){
var js, id = 'facebook-jssdk'; if (d.getElementById(id)) {return;}
js = d.createElement('script'); js.id = id; js.async = true;
js.src = "//connect.facebook.net/en_US/all.js";
d.getElementsByTagName('head')[0].appendChild(js);
}(document));
</script>
最佳答案
更好的做法是在用户注销时使用重定向,而不是 POST,即:
HTML:
<a onclick="FB.logout">Logout</a>
和 JavaScript:
FB.Event.subscribe('auth.authResponseChange', function(response) {
if (response.status == "connected") {
// log in login
}
else if (response.status != "connected") {
// logout logic, i.e
// location.href = "/sessions/logout";
alert("User has disconnected")
location.href = "/index.php?fbconnect=fbconnect"
}
});
如果你坚持要发表一篇文章,你可以使用 jQuery 来简化事情:
FB.Event.subscribe('auth.login', function(response) {
$.post("index.php", {fbconnect: fbconnect}, function(data, response) {
alert(data);
});
});
关于php - 添加POST参数到fb :login button,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16777760/