Last Updated: February 25, 2016
· wmichaelgreen

PHP Facebook Like Gate

Here is a simple PHP function to create a conditional page based on whether or not the user likes the Facebook page or not. Also, it makes use of the javascript SDK to dynamically resize the canvas to the desired width and height of the page content.

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Site Name</title>
<meta property="og:title" content=""/>
<meta property="og:type" content="sport"/>
<meta property="og:url" content=""/>
<meta property="og:image" content="/images/appLogo.jpg"/>
<meta property="og:site_name" content="Site Name"/>
<meta property="fb:admins" content="USER_ID"/>
<meta property="og:description" content="Description."/>
<meta property="fb:app_id" content="APP ID"/>

<link rel="stylesheet" href="styles/site.css" type="text/css" />

<body style="">
<div id="fb-root"></div>
<script src=""></script>
    appId  : '123456789', // unique app ID for tab application. This will need to be updated for live app.
    status : true, // check login status
    cookie : true, // enable cookies to allow the server to access the session
    xfbml  : true, // parse XFBML
    oauth  : true // enable OAuth 2.0


function parse_signed_request($signed_request) {
  list($encoded_sig, $payload) = explode('.', $signed_request, 2);

  // decode the data
  $sig = base64_url_decode($encoded_sig);
  $data = json_decode(base64_url_decode($payload), true);

  if (strtoupper($data['algorithm']) !== 'HMAC-SHA256') {
    error_log('Unknown algorithm. Expected HMAC-SHA256');
    return null;

  return $data;

function base64_url_decode($input) {
  return base64_decode(strtr($input, '-_', '+/'));

  if($signed_request = parse_signed_request($_REQUEST['signed_request'])) {
    if($signed_request["page"]["liked"]) {
<script type="text/javascript">
        FB.Canvas.setSize({ width: 520, height: 1250 }); // Set to the correct height of your page content.
<div id="like">
   You liked this.
    } else {
<script type="text/javascript">
        FB.Canvas.setSize({ width: 520, height: 530 }); // Set to the correct height of your page content.
<div id="preLike">
      Like this page.
  else {
No signed_request. I am broken.