php - 添加POST参数到fb :login button

标签 php javascript facebook

您好,我尝试在用户单击“使用 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/

相关文章:

facebook - 登录时未收到来自 facebook API 的电子邮件

android - 在 Android 上使用 Facebook API 创建自定义墙贴

php - 从多个表中选择并返回全部

JavaScript/jQuery 上下文 - 代理?

php - 如何在 MAC 中安装 Laravel 的 mssql 驱动程序 (sqlsrv)?

javascript - 以逗号分隔的电子邮件 - 添加邮件长度检查(JS 正则表达式)

javascript - 如何在没有任何 html5 功能的情况下通过 javascript 播放 mp3 文件?

html - 获取 FAN 页面上的好友列表,而不是个人页面

php - 如何在 PHP 中检索 SAML2 token 以从 WSO2 APIM 获取 OAuth token

php - 获取某人登录计算机时使用的名称