Using Facebook as a login system having weird problems
===

I'm just trying to get some basic stuff with the Facebook API working but I keep getting the error -  Uncaught OAuthException: An active access token must be used to query information about the current user - even though I get redirected to an authentication page on facebook...As well, probably due to this, the value of $facebook->getUser is always zero. I know there are some posts on this but I can't seem to figure out what I'm doing wrong, I'm basically using the EXACT code given as example..:

    require_once("facebook.php");

    $facebook = new Facebook(array(
       'appId' => APP_ID, //these are both defined above in my code
       'secret' => APP_SECRET,
       'cookie' => true,
    ));

    // Get User ID
    $user = $facebook->getUser();
    echo $user;

    // 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->api('/me');
    echo $user_profile->name;
    } catch (FacebookApiException $e) {
    error_log($e);
    $user = null;
    }
    } else {
	$login_url = $facebook->getLoginUrl($params = array('redirect_uri' => REDIRECT_URI,'scope' => PERMISSIONS_REQUIRED));
	    $user_profile = $facebook->api('/me');
	    echo $user_profile->name;
		   // echo ("<script> top.location.href='".$login_url."'</script>");
    }

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