title: Facebook PHP SDK session variable not passing
body: <p>fbconnect.php</p>
  
  <pre><code>include('functions.php');
  connect();
  
  require 'src/facebook.php';
  
  if (!isset($_SESSION['user'])) {
  
  // Create our Application instance (replace this with your appId and secret).
  $facebook = new Facebook(array(
    'appId'  =&gt; 'xxx',
    'secret' =&gt; 'xxx',
  ));
  
  // Get User ID
  $user = $facebook-&gt;getUser();
  
  // We may or may not have this data based on whether the user is logged in.
  //
  // If we have a $user id here, it means we know the user is logged into
  // Facebook, but we don't know if the access token is valid. An access
  // token is invalid if the user logged out of Facebook.
  
  if ($user) {
    try {
      // Proceed knowing you have a logged in user who's authenticated.
      $user_profile = $facebook-&gt;api('/me', 'GET');
  
  $ret = $facebook-&gt;api( array(
                           'method' =&gt; 'fql.query',
                           'query' =&gt; 'SELECT affiliations FROM user WHERE uid = me()',
                       ));
  
  $network = $ret['0']['affiliations']['0']['name'];
  //echo $network;
  
  $data = $facebook-&gt;api('/me', 'get', array("fields"=&gt;"location"));
  $location = $data['location']['name'];
  
  $sql = mysql_query("SELECT * FROM xx_users WHERE user_id=$user_profile[id]");
  
  if (mysql_num_rows($sql) == 0) {
  
  
  mysql_query("INSERT INTO xx_users (name, user_id, email, network, location)
  VALUES ('$user_profile[name]', '$user_profile[id]', '$user_profile[email]', '$network', '$location')");
  
  }
  
  else {
  
  
  }
  
  
    } catch (FacebookApiException $e) {
      error_log($e);
      $user = null;
    }
  }
  
  
  // Login or logout url will be needed depending on current user state.
  if ($user) {
    $logoutUrl = $facebook-&gt;getLogoutUrl();
  } else {
  //  $loginUrl = $facebook-&gt;getLoginUrl();
  $loginUrl = $facebook-&gt;getLoginUrl(array(
          'scope'     =&gt; 'email, user_location, user_work_history, user_education_history', // Permissions to request from the user
          'redirect_uri'  =&gt; 'http://comfyshoulderrest.com/socialexchange/home.php', // URL to redirect the user to once the login/authorization process is complete.
          ));
  
  }
  }
  
  else {
  $_SESSION['user'] = $user;
  $_SESSION['friends'] = $facebook-&gt;api('/me/friends');
  // $_SESSION['friends'] = $facebook-&gt;api('/$user/friends'); THIS GIVES THE SAME RESULT
  }
  </code></pre>
  
  <p>data.php</p>
  
  <pre><code>include('functions.php');
  connect();
  session_start();
  
  $friends = $_SESSION['friends'];
  $user = $_SESSION['user'];
  
  $arr = $friends['data'];
  $friend_ids_arr = array();
  foreach ($arr as $friend) { // THIS IS LINE 14
      $friend_ids_arr[] = $friend['id'];
  }
  </code></pre>
  
  <p>error:</p>
  
  <pre><code>Warning: Invalid argument supplied for foreach() in /blah/data.php on line 14
  </code></pre>
  
  <p>Is there anything I'm doing that's preventing the variable from passing?</p>
  
  <p><strong>EDIT</strong></p>
  
  <p><code>$_SESSION['user']</code> prints just fine, so I presume the problem lies with this line:</p>
  
  <p><code>$_SESSION['friends'] = $facebook-&gt;api('/me/friends');</code></p>