2013-03-23 144 views
0

我想在我的網站中按鈕onclick將在用戶的Facebook牆上張貼消息。 我從我的網站中的PHP頁面調用了一個AJAX腳本,它將_POST中的消息用facebook代碼發送到php文件。Facebook上的會話問題PHP SDK

我得到這個錯誤,而運行我的PHP文件:

警告:在session_start()[function.session啓動]:不能發送 會話緩存限制器 - 已經發送了頭(輸出開始 /home/user/public_html/fb/index.php:1)在 /home/user/public_html/fb/facebook.php上線49

我的PHP是這樣的:

<?php 
include('../ilink.php'); //mysqli connection 

$fb_message=$_POST['fb_message']; 
$fb_description=$_POST['fb_description']; 
$fb_picture=$_POST['fb_picture']; 


require 'facebook.php'; 

// Create our Application instance. 
$facebook = new Facebook(array(
    'appId' => '--appid--', 
    'secret' => '--secret--', 
    'cookie' => true 
)); 

    $user = $facebook->getUser(); 

    if ($user) { 
     try { 
     // Proceed knowing you have a logged in user who's authenticated. 
     $user_profile = $facebook->api('/me'); 
     } catch (FacebookApiException $e) { 
     error_log($e); 
     $user = null; 
     } 
    } 

    // Login or logout url will be needed depending on current user state. 
    if ($user) { 
     $logoutUrl = $facebook->getLogoutUrl(); 
    } else { 
     $loginUrl = $facebook->getLoginUrl(); 
    } 

    $attachment = array('message' => $fb_message, 
      'name' => 'text', 
      'caption' => 'text', 
      'link' => 'http://www.domain.com', 
      'description' => $fb_description, 
      'picture' => $fb_picture, 
      'actions' => array(array('name'=>'text', 
           'link' => 'http://www.domain.com'),) 
      ); 

    $result = $facebook->api('/me/feed/','post',$attachment); 
?> 

我的AJAX腳本:

function fb_post(fb_message,fb_description,fb_picture,redirect){ 
     var xmlhttp; 
     if (window.XMLHttpRequest){// code for IE7+, Firefox, Chrome, Opera, Safari 
      xmlhttp=new XMLHttpRequest(); 
     } 
     else{// code for IE6, IE5 
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
     } 
     xmlhttp.onreadystatechange=function(){ 
      if (xmlhttp.readyState==4 && xmlhttp.status==200){ 

       document.location.href =redirect;    
      } 
     } 

     var q="fb_message="+fb_message+"&fb_description="+fb_description+"&fb_picture="+fb_picture; 
     xmlhttp.open("POST","/fb/index.php",true); 
     xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); 
     xmlhttp.send(q); 
} 

爲什麼錯誤出現在哪裏?

+0

由於當身體部位發送要求頭部再次發送時出現此錯誤。請從頁面頂部的所有文件中刪除任何空間 – 2013-03-23 19:22:12

回答

0

如果這是您的所有代碼,它看起來像包含文件中的某些內容在會話實例之前發送了頭文件。

+0

我在我的網站上的每個頁面都有Session(用於用戶登錄)。 facebook.php上有會話。也許我需要以某種方式銷燬會話? Rohit Kumar - 我沒有空格。 – 2013-03-24 07:05:07

+0

你在每個頁面上的含義是什麼,你不必在每一頁上開始會話,顯示更多的代碼。 – robinloop 2013-03-24 15:33:58

+0

我在每個頁面中都包含(username.php)konwing誰是現在登錄的用戶,此頁面以下列行開始:<?php session_start(); $ user_id = $ _ SESSION ['logon']; include(「ilink.php」); – 2013-03-24 17:28:59