Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7188841
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T19:10:20+00:00 2026-05-28T19:10:20+00:00

I don’t understand the code that comes after The following PHP example demonstrates the

  • 0

I don’t understand the code that comes after “The following PHP example demonstrates the server-side flow with CSRF protection in one self-contained example:” at http://developers.facebook.com/docs/authentication/ i.e Why is it needed?
Why session_start(); is needed? I don’t understand where the work with the session begins or ends.
How does the CSRF protection work?
Why access token is not returned right after user login?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T19:10:21+00:00Added an answer on May 28, 2026 at 7:10 pm

    You call session_start() once at the top of your script, before anything it printed out.

    After that you have access to the $_SESSION array. This allows you to store values like $_SESSION['state'] from one page call to another.

    The code in the example shows a CSRF protection. The first time you call tt stores a random value in the session and compares it afterwards.

    Read more about php sessions.

    Update Script with comments. If you have a look at the picture above the script… I “marked” some points from there.

       // Set your facebook config here
       $app_id = "YOUR_APP_ID";
       $app_secret = "YOUR_APP_SECRET";
       $my_url = "YOUR_URL";
    
       // start session to store random state
       session_start();
       // get a code from the request
       $code = $_REQUEST["code"];
    
       // if no code was send to the script...
       if(empty($code)) {
         // generate a random, unique id and store it the session
         $_SESSION['state'] = md5(uniqid(rand(), TRUE)); //CSRF protection
         // create facebook dialog url
         $dialog_url = "https://www.facebook.com/dialog/oauth?client_id=" 
           . $app_id . "&redirect_uri=" . urlencode($my_url) . "&state="
           . $_SESSION['state'];
    
         // redirect user to facebook
         // Facebook login and App Permissions request
         // "GET OAuth Dialog"
         echo("<script> top.location.href='" . $dialog_url . "'</script>");
       }
    
       // CSRF protection: check if state parameter is the same as 
       // we stored it in the session before the redirect
       if($_REQUEST['state'] == $_SESSION['state']) {
         // do facebook auth "GET /oauth/authorize"
         $token_url = "https://graph.facebook.com/oauth/access_token?"
           . "client_id=" . $app_id . "&redirect_uri=" . urlencode($my_url)
           . "&client_secret=" . $app_secret . "&code=" . $code;
    
         $response = @file_get_contents($token_url);
         $params = null;
         parse_str($response, $params);
    
         // "GET me?access_token"
         $graph_url = "https://graph.facebook.com/me?access_token=" 
           . $params['access_token'];
    
         $user = json_decode(file_get_contents($graph_url));
         echo("Hello " . $user->name);
       }
       else {
         echo("The state does not match. You may be a victim of CSRF.");
       }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Don't think that I'm mad, I understand how php works! That being said. I
I don't understand why this code doesn't work: function Messages(type,text) { console.log(In function Message);
Don't let below code scare you away . The question is really simple, only
Don't be scared of the extensive code. The problem is general. I just provided
I don't understand where the extra bits are coming from in this article about
I don't want PHP errors to display /html, but I want them to display
Don't be frightened, its a very basic code. Just wanted to check with you
Don't they both have to convert to machine code at some point to execute
I don't know how better to describe this. So take this example. var a
I don’t think I’ve grokked currying yet. I understand what it does, and how

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.