2011-07-15 37 views
1

我有下面的代碼,讓我發佈一條消息到用戶的Facebook牆上當前登錄:如何阻止人們訪問此頁面?

<?php 
    error_reporting(E_ALL | E_STRICT); 
    ini_set('display_errors', 1); 

    session_start(); 

    require("facebook.php"); 

    $facebook = new Facebook(array(
     'appId' => 'app_id_here', 
     'secret' => 'secret_here', 
     'cookie' => true 
    )); 

    $session = $facebook->getSession(); 

    if(!empty($session)) { 

     try { 

      $uid = $facebook->getUser(); 
      $user = $facebook->api('/me'); 

      $api_call = array( 
       'method' => 'users.hasAppPermission', 
       'uid' => $uid, 
       'ext_perm' => 'publish_stream' 
      ); 

      $can_post = $facebook->api($api_call); 

      if($can_post) { 

       $facebook->api('/'.$uid.'/feed', 'post', array('message' => 'post some message here')); 
       echo 'Posted! Then redirect to an appropriate page.'; 

      } else { 

       $url = $facebook->getLoginUrl(array( 
        'req_perms' => 'publish_stream' 
       )); 

       header("Location: {$url}"); 
       exit; 

      } 

     } catch (Exception $e) { 

      echo $e; 
      exit; 

     } 

    } else { 

     $url = $facebook->getLoginUrl(array( 
      'req_perms' => 'publish_stream' 
     )); 

     header("Location: {$url}"); 

    } 
?> 

我需要用我的網站在多個地方的代碼,並且需要不斷更改消息被張貼。所以我想我應該使用url查詢字符串將消息發送到上面的代碼。即

header(「Location:post_message.php?message =」。$ some_message_here);

但是,這將意味着任何人找出網址將能夠開始張貼隨機信息的權利?即在這裏

如何在我需要的所有頁面中使用上面的代碼

http://mysite.com/post_message.php?message=some消息,它傳遞一個字符串被張貼的消息,但阻止用戶導航到該頁面後隨機消息?

回答

7
  1. 把該文件的根目錄
  2. 包裹代碼之外的功能
  3. 如果你想使用它,它include並調用該函數。
+0

我已經將它包裝在一個函數中,並將它放在'/ var/functions/post_facebook_message.php'中。訪問它的文件位於'/ var/www/folder1/post_message.php',我不知道如何在'post_message.php'文件中包含'post_facebook_message.php'?我試過'include(「../../ functions/post_facebook_message.php」)' – nami

+0

'include('/ var/functions/post_facebook_message.php');' – Quentin

+0

這很好用,謝謝。所以這是一條絕對的道路。你如何使用相對路徑? – nami

0

在這種情況下,登錄用戶將能夠從您的應用程序發佈任何消息到他們的牆上。如果消息不是用戶生成的,那麼我不明白爲什麼它不會是有限數量的消息。

我建議你定義你的消息並將標識符傳遞給那個消息。如果消息要有可變內容,那麼也可以傳遞該消息。我將定義一個包含唯一標識符令牌的所有預期參數的消息對象,並對其進行JSON編碼並將其作爲get請求中的唯一變量傳遞給它。