将POST参数添加到fb:login按钮

时间:2013-05-27 17:15:38

标签: php javascript facebook

你好我试图在用户用facebook按钮点击登录时设置一个POST ..这是因为在我制作的代码中,如果你已经注销,但是在Facebook中插入,那么你会在网站中再次被盗...所以,如果你注销,你永远不会注销..

为了避免这种情况我试图设置POST,设置经典条件

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

我只需添加一个隐藏的输入并发送该表单usinf 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();表单,但它可以正常工作

我也试过这个

<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>

1 个答案:

答案 0 :(得分:1)

更好的做法是在用户注销时使用重定向,而不是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);
  });

});