添加一个POST参数到fb:login按钮

Add a POST parameter to fb:login button

本文关键字:fb login 按钮 参数 POST 一个 添加      更新时间:2023-09-26

你好,我试图设置一个POST当用户点击login with facebook按钮。这是因为在我所做的代码中,如果你注销了,但在facebook上登录,那么你会自动再次登录网站…所以如果你点击注销,你永远不会注销。

为了避免这种情况,我尝试设置一个POST,设置一个经典的条件

 if ( isset ($_POST['fbconnect'])) 

我只需要添加一个隐藏的输入和发送表单使用: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>

我也试着用form的名字替换$(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);
  });
});