title: How to get user email from facebook authentication example via php or javascript
body: <p>I have a photo gallery on my website and I want to require facebook authentication in order to view it and collect names/email addresses.  So far I have went [here][1] and read that I tried the php example which worked but I am confused on how to get the users name/email when authenticating? I used this but in the example why would "code" be empty and how can I get the user's info like how in the example its getting the "state".  Also in my version it just fires off the redirect, how come in the example the 2nd condition is never met?</p>
  
  <p>The example:</p>
  
  <pre><code> &lt;?php 
  
     $app_id = "YOUR_APP_ID";
     $app_secret = "YOUR_APP_SECRET";
     $my_url = "YOUR_URL";
  
     session_start();
     $code = $_REQUEST["code"];
  
     if(empty($code)) {
       $_SESSION['state'] = md5(uniqid(rand(), TRUE)); //CSRF protection
       $dialog_url = "http://www.facebook.com/dialog/oauth?client_id=" 
         . $app_id . "&amp;redirect_uri=" . urlencode($my_url) . "&amp;state="
         . $_SESSION['state'];
  
       echo("&lt;script&gt; top.location.href='" . $dialog_url . "'&lt;/script&gt;");
     }
  
     if($_REQUEST['state'] == $_SESSION['state']) {
       $token_url = "https://graph.facebook.com/oauth/access_token?"
         . "client_id=" . $app_id . "&amp;redirect_uri=" . urlencode($my_url)
         . "&amp;client_secret=" . $app_secret . "&amp;code=" . $code;
  
       $response = @file_get_contents($token_url);
       $params = null;
       parse_str($response, $params);
  
       $graph_url = "https://graph.facebook.com/me?access_token=" 
         . $params['access_token'];
  
       $user = json_decode(file_get_contents($graph_url));
       echo("Hello " . $user-&gt;name);
     }
     else {
       echo("The state does not match. You may be a victim of CSRF.");
     }
  
   ?&gt;
  </code></pre>