No active access token with ajax based app

I am developing an app with 2 files:

  1. index.php (application container)
  2. process.php (called in ajax)

index.php

<?php
    include 'config.php';
    include 'lib/functions.php';
    require_once 'lib/sdk/facebook.php';

    $facebook = new Facebook(array(
        'appId'  => APP_ID,
        'secret' => SECRET
    ));

    $userID = $facebook->getUser();

    if (!$userID)
    {
         $scope = 'read_stream, publish_stream, user_likes, user_photos, user_status, friends_hometown, friends_location, friends_likes, photo_upload';

         $params = array(
             'scope'        => $scope,
             'redirect_uri' => "https://apps.facebook.com/".APP_NAME."/"
         );

         $loginUrl = $facebook->getLoginUrl($params);
    }
    else 
    {
        $access_token = $facebook->getAccessToken();
    }
?>
<!doctype html>
<html xmlns:fb="http://www.facebook.com/2008/fbml">
    <head>
        <title>Facebook App</title>
        <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
        <link href="/css/style.css" rel="stylesheet" type="text/css" media="screen" />
        <script type="text/javascript" src="/js/jquery.js"></script>
    </head>

    <body style="margin:0 !important; padding: 0 !important;">

        { SOME HTML }

        <script type="text/javascript">
             function loadData()
             {
                  $.ajax({
                      url: "process.php",
                      type: 'POST',
                      cache: false,
                      data: {
                          id_sessione: function(){return $("#id_sessione").val();}
                      },
                      dataType: "json",
                      success: function(data) {
                          if (data.status == "FAILURE")
                              alert('failure');
                          else
                              console.log(data);
                      }
                  });
             }




             /********* INIT ********/
             $(document).ready(function(){
                 if ($("#userID").val() != "0")
                     loadData();
                 else
                     top.location = '<?php echo $loginUrl; ?>';
             });
         </script>

         <input id="id_sessione" type="hidden" value="<?php echo session_id(); ?>" />
         <input id="userID" type="hidden" value="<?php echo $userID; ?>" />

         <div id="fb-root"></div>
         <script type="text/javascript">
             window.fbAsyncInit = function() {
                 FB.init({
                      appId: '<?php echo APP_ID; ?>',
                      frictionlessRequests: true
                 });

                 FB.Canvas.setAutoResize(7);
             };

             (function() {
                 var e = document.createElement('script');
                 e.async = true;
                 e.src = document.location.protocol +
                     '//connect.facebook.net/it_IT/all.js';
                     document.getElementById('fb-root').appendChild(e);             
             }());
          </script>
      </body>
 </html>

and process.php

<?php
    session_id($_POST['id_sessione']);
    session_start();

    include 'config.php';
    include 'lib/functions.php';
    require_once 'lib/sdk/facebook.php';

    set_time_limit(120);
    $memory_limit = ini_set('memory_limit', '512M');

    try
    {
        $facebook = new Facebook(array(
            'appId'  => APP_ID,
            'secret' => SECRET
        ));

        $userID = $facebook->getUser();

        $user_profile = $facebook->api('/me');

        { SOME FQL QUERIES AND DATA PROCESSING }

        echo json_encode(array(
            'status' => 'SUCCESS',
            .... other info
        ));
     } catch (Exception $e) {

         $handle = fopen(ROOT_PATH.'/log/'.$userID.'_'.uniqid().'.txt', 'a+') or die('Cannot open file');
         fwrite($handle, $e->getMessage()."\n");
         fwrite($handle, $e->getTraceAsString()."\n");
         fclose($handle);

         echo json_encode(array(
             'status'   => 'FAILURE'
         ));
      }

In 95% of cases application works fine but sometimes $facebook->api('/me'); trows the exception "An active access token must be used to query information about the current user." and the $userID is equal to 0.

In order to have the same session id in all files I have to start manually session with the session_id passed by index.php in POST. This is because some browser (IE, safari) don't pass cookies of third party pages in iframe. (http://stackoverflow.com/questions/9930671/safari-3rd-party-cookie-iframe-trick-no-longer-working)

Any suggestions? Thank you

Answers


check whether the session value is passing properly or not?


Need Your Help

Drawing with two CALayers on top of each other

ios cocoa-touch core-graphics calayer

I have 2 views and I want to use their respective layers for drawing. My first view, which I'll call the TextView, is nothing but a page full of text. The other view, which I'll call DrawView, is w...