Determine if request is coming from within Facebook tab and remember it without sessions (and FB SDK)

Submitted by flei - 9 years ago

I use this to always know in my controllers if the current action is shown through a Facebook tab iframe. Upon the first request this checks if there is a "signed_request" variable posted to the page. Then $fb_state is set to true and shared with all views. The problem is that on navigating to the next page within your app this variable is lost and you don't know if this is shown through facebook. Due to the FB page proxy you can't use sessions to remember this for the next request either. I solve this problem with adding "signed_request" as a parameter (i.e. "&signed_request=123ljshgljsdhflsdhfl") to every link and form when $fb_state == true. Of course you could also pass any other variable if you don't need the actual signed_request data.

// put this in your base controller so it's at hand in every child controller
class BaseController extends Controller {

    public $fb_state = false;
	
	function __construct() {

		// check if called through facebook tab
		$fb_signed_request = '';

		if (Input::get('signed_request')) {
			$this->fb_state = true;
			$fb_signed_request = Input::get('signed_request');
		}
		
		View::share('fb_signed_request', $fb_signed_request);
		View::share('fb_state', $this->fb_state);

	}
}