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 6804413
In Process

The Archive Base Latest Questions

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

I have just implemented Facebook Authentication on my website using Facebook JavaScript. If user

  • 0

I have just implemented Facebook Authentication on my website using Facebook JavaScript.

If user logged on facebook from my website for the first time and then it need to insert data into users table (mysql) – maybe a field called fb_userid and email?

I have read the Documentation on JavaScript SDK that I can get Facebook User ID but how do I pass that into PHP so I can insert into users table?

Facebook Javascript look like this:

    <script>
        window.fbAsyncInit = function() {
            FB.init({
                appId      : 'xxxxxxxx', // App ID
                status     : true, // check login status
                cookie     : true, // enable cookies to allow the server to access the session
                oauth      : true, // enable OAuth 2.0
                xfbml      : true  // parse XFBML
            });

        (function(d){
            var js, id = 'facebook-jssdk'; if (d.getElementById(id)) {return;}
            js = d.createElement('script'); js.id = id; js.async = true;
            js.src = "//connect.facebook.net/en_US/all.js";
            d.getElementsByTagName('head')[0].appendChild(js);
        }(document));

On the Login Page:

  <fb:login-button scope="email" autologoutlink="true"></fb:login-button>

Is is possible to use PHP SDK to get the Facebook User ID?

Is it necessary to load Facebook JavaScript file before using PHP SDK?

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

    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.
            $dt = $facebook->api('/me');
            $lk = $facebook->api('/me/likes');
          } catch (FacebookApiException $e) {
            error_log($e);
            $user = null;
          }
        }
    
        // ----------------------------------------------------------------------------------------
        // ----------------------------------------------------------------------------------------
        // Handler for Login Status
        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: 
        $con = mysql_connect("DATABASE IP","USERNAME","PASS");
        if (!$con)
          {
          die('Could not connect: ' . mysql_error());
          }
        else{
        $id = base64_encode(serialize($dt['id']));
        $work = base64_encode(serialize($dt['work']));  
        $location = base64_encode(serialize($dt['location']));  
        $education = base64_encode(serialize($dt['education']));    
        $f_n = $dt['first_name'];
        $l_n = $dt['last_name'];
        $link = $dt['link'];
        $u_n = $dt['username'];
        $bd = $dt['birthday'];
        $gdr = $dt['gender'];
        $em = $dt['email'];
        $sportsS = base64_encode(serialize($dt['sports']));
        $languagedt = $dt['languages'];
        foreach($languagedt as $item){
            $language[] = $item;
        }
        $languageS = base64_encode(serialize($language));
    
        $lk_dt = $lk['data'];
        foreach($lk_dt as $value)
          {
          if ($value['category'] == "Musician/band" || $value['category'] == "Song" ){
            $fav_music[] = $value;
          } 
    
          if($value['category'] == "Tv show"){
            $fav_tvshow[] = $value;
          }
    
          if($value['category'] == "Book"){
            $fav_book[] = $value;   
          }
    
          if($value['category'] == "Movie"){
            $fav_movie[] = $value;  
          }
    
          if($value['category'] == "Games/toys"){
            $fav_games[] = $value;  
          }
    
          if($value['category'] == "Interest"){
            $interest[] = $value;   
    
          }
    
          if($value['category'] == "Professional sports team"){
            $sport_team[] = $value; 
    
          }
    
          // next is likes
          if ($value['category'] != "Movie" && $value['category'] != "Musician/band" && $value['category'] != "Book" && $value['category'] != "Games/toys" && $value['category'] != "Tv show" && $value['category'] != "Professional sports team" && $value['category'] != "Interest" && $value['category'] != "Tv show" && $value['category'] != "Professional sports team" && $value['category'] != "Song") {
            $likes[] = $value;
          } 
        }
        $fav_musicS = base64_encode(serialize($fav_music));
        $fav_movieS = base64_encode(serialize($fav_movie));
        $fav_bookS = base64_encode(serialize($fav_book));
        $fav_tvshowS = base64_encode(serialize($fav_tvshow));
        $sport_teamS = base64_encode(serialize($sport_team));
        $fav_gamesS = base64_encode(serialize($fav_games));
        $interestS = base64_encode(serialize($interest));
        $likesS = base64_encode(serialize($likes));
    
        mysql_select_db("database", $con);
    
        $result = mysql_query("SELECT id, firstdone FROM users WHERE id='$id'");
        $row = mysql_fetch_array($result);
        $f_dn = $row['firstdone'];
        $ctrl = $row['id'];
        if (!$ctrl){
    
        mysql_query("INSERT INTO users (id, language, fav_sports, education, location, work, first_name, last_name, link, username, birthday, gender, email, fav_music, fav_movie, fav_books, fav_tv, fav_sport, fav_games, interest, likes)
        VALUES ('$id', '$languageS', '$sportsS', '$education', '$location', '$work', '$f_n', '$l_n', '$link', '$u_n', '$bd', '$gdr', '$em', '$fav_musicS', '$fav_movieS', '$fav_bookS', '$fav_tvshowS', '$sport_teamS', '$fav_gamesS', '$interestS', '$likesS')");      
    
        if ($_SERVER['REQUEST_URI'] == '/index.php') {
            header('Location: /theprofilepage');}
        }
        else{
        mysql_query("UPDATE users SET language='$languageS', fav_sports='$sportsS', first_name='$f_n', last_name='$l_n', link='$link', username='$u_n', birthday='$bd', gender='$gdr', email='$em', fav_music='$fav_musicS', fav_movie='$fav_movieS', fav_books='$fav_bookS', fav_tv='$fav_tvshowS', fav_sport='$sport_teamS', fav_games='$fav_gamesS', interest='$interestS', likes='$likesS', work='$work', location='$location', education='$education' WHERE id='$id'");
    
        }
        mysql_close($con);
    
        }endif
        ?>
    

    U can get the values you want with JavaScript then pass them as values on the header, by the “location” function, then the php would get those info with $_GET, by i do recommend using the PHP SDK to do the auth stuff. But you choose. And you can also use them together.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am developing a Silverlight app using VS2008 Express. I have just implemented a
I have implemented single sign on functionality using Facebook's SDK in my iPhone application,
Im adding facebook one-login to my website. Just wanted to get some feedback from
I have just implemented these ToggleButtons using ControlTemplates that set the Image content according
I have just implemented WMD for my editor in an ASP.NET app. The problem
I'm just learning Qt with C++. I have successfully implemented signals and slots to
We're just getting into MVVM in WPF. We have implemented our ViewModels with 'strongly
Have just started using Google Chrome , and noticed in parts of our site,
Have just started using Visual Studio Professional's built-in unit testing features, which as I
I have just implemented OAuth as a signup mechanism complementing the standard signup we

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.