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

  • Home
  • SEARCH
  • 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 6805407
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T19:34:57+00:00 2026-05-26T19:34:57+00:00

I am using the code from the facebook app developer site. I tried it

  • 0

I am using the code from the facebook app developer site. I tried it and I am getting the following error :

"The state does not match. You may be a victim of CSRF"

The code used is as follows :

  <?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 . "&redirect_uri=" . urlencode($my_url) . "&state="
       . $_SESSION['state'];

     echo("<script> top.location.href='" . $dialog_url . "'</script>");
   }

   if($_REQUEST['state'] == $_SESSION['state']) {
     $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);

     $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.");
   }

 ?>

From the code its obv that for some reason, $_REQUEST[‘state’] != $_SESSION[‘state’]. Can somebody please explain why this is happening. I am also a PHP amateur.

Thank u 🙂

  • 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-26T19:34:58+00:00Added an answer on May 26, 2026 at 7:34 pm

    Use SDK, much easier.

    This code gets the user data and serializes it, and throws to the database, i know its not perfect but have a look. I’m going to edit this when i get some free time, then i recommend encoding the users data as JSON, not as base64 serialized, because it will be easier to do query searches in the future.

        <?php
            require 'facebook.php'; // USE FACEBOOK PHP SDK
    
            // Create our Application instance (replace this with your appId and secret).
            $facebook = new Facebook(array(
              'appId'  => 'APPID',
              'secret' => 'APPSECRET',
            ));
            // ----------------------------------------------------------------------------------------
            // ----------------------------------------------------------------------------------------
    
            // Get User ID
            $user = $facebook->getUser();
    
            /* 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.
                // these are the graph calls
                $dt = $facebook->api('/me');
                $lk = $facebook->api('/me/likes');
              } catch (FacebookApiException $e) {
                error_log($e);
                $user = null;
              }
            }
    
            // ----------------------------------------------------------------------------------------
            // ----------------------------------------------------------------------------------------
            // Handler for Login Status
            // With the LoginURL, user the 'scope' to ask for permissions
            if ($user) {
              $logoutUrl = $facebook->getLogoutUrl();
            } else {
              $loginUrl = $facebook->getLoginUrl(array("scope" => "email,user_birthday,user_likes,user_work_history,user_location,user_education_history"));
            }
            // ----------------------------------------------------------------------------------------
            ?>
            <?php if (!$user): header ('Location:'.$loginUrl.''); //CHECKS IF USER IS LOGGED IN
            else: 
    
     // Do Something here. This next bit of code shows what comes out of those calls.
          echo "<pre>"; 
          print_r($dt);
          echo"</pre>";
    
          echo"<br/><br/>";
    
          echo "<pre>"; 
          print_r($lk);
          echo"</pre>";
    
          endif
            ?>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to get JSON data from facebook using the following code: var getFriends
Using the code below (from a console app I've cobbled together), I add seven
I have tried following the FB mobile web getting started guide at: https://developers.facebook.com/docs/guides/mobile/web/ for
I'm trying to setup a new facebook app using the example from Facebook but
My main solution is using code from a utility class library, that I wrote
I am attempting to construct my own date picker using code from several sources.
I am attempting to construct my own date picker using code from several sources.
I'm using this code from Microsoft to play audio notifications for my application. It's
I'm using some code from here to determine when determining when the last finger
A bit of background first: I am using base code from a remote SVN

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.