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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T19:32:35+00:00 2026-05-24T19:32:35+00:00

I’m using Facebook’s PHP SDK to log users in to my website, like so:

  • 0

I’m using Facebook’s PHP SDK to log users in to my website, like so:


<?php
$facebook = new Facebook(array(
    "appId"  => "myAppID",
    "secret" => "mySecret",
));

$user = $facebook->getUser();

if ($user) {
    try {
        // user is authenticated
        $user_profile = $facebook->api("/me");
    } catch (FacebookApiException $ex) {
        $user = null;
    }
}

if ($user) {
    $logoutURL = $facebook->getLogoutUrl();
    ?>
    <img src="https://graph.facebook.com/<?php echo $user; ?>/picture" />
    <a href="<?php echo($logoutURL); ?>">Logout</a>
    <pre><?php print_r($user_profile); ?></pre>
    <?php
} else {
    ?>
    <a href="<?php echo($loginURL); ?>">Login</a>
    <?php
}
?>

The problem is that when a user returns to the site, they’re no longer logged in, and have to log back in.

Is it possible for returning users to remain logged in?


Update:

My exact solution ended up being a combination of the JavaScript and PHP Facebook SDKs.

The problem is that a cookie isn’t used to store the Facebook login information—a PHP session is (PHP session cookies expires when the browser is closed). In addition to this, unless you have offline access, the access tokens expire. In other words, even if you were to store the Facebook access token in a cookie, it would eventually expire and the user would be logged out.

The JavaScript I ended up using:

    FB.init({
        appId: "myAppID",
        cookie: true, // enable cookies to allow the server to access the session
        xfbml: true, // parse XFBML
        oauth: true // enable OAuth 2.0
    });

    FB.Event.subscribe("auth.login", function(response) {
        // a user logged in who was not previously logged in
        window.location.refresh(true); // ideally modify the page with JS rather than refreshing
    });

    FB.Event.subscribe("auth.logout", function(response) {
        // a user logged out who was previously logged in
        window.location.refresh(true); // ideally modify the page with JS rather than refreshing
    });

    // check if the user is logged in or not, using the Facebook JS SDK
    FB.getLoginStatus(null);

When the page is loaded, the Facebook JavaScript SDK checks with Facebook to see if the user is logged in. If the user is logged in, and the cookie isn’t already set, it will be set. You can then refresh the page (or, ideally, modify it with JavaScript) and the server will be able to access the cookie using the regular PHP SDK (and the first piece of code in this question).

This is the best way I’ve found for persistent logins across browsing sessions.

  • 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-24T19:32:37+00:00Added an answer on May 24, 2026 at 7:32 pm

    There are two scenarios here. One is to authenticate the user, and the other one is to recognize the user.

    What usually happens when you use the login methods provided from any Facebook API is that they will see if the user have a session going with Facebook. If not the user will be prompted to log in so you can see which user it is. In your case you want the user to always be logged in to Facebook. This is not really doable since you need to find out which user it is. You could store a cookie on the users computer with some recognition details. You would need to have offline permissions for the user or else the user will have to be logged in with the API to get a fresh access_token.

    If you want my advise you should just use the GetLoginStatus method of the API to check if the user has a session going with Facebook. If the user is not logged in to Facebook it is not too much to require them to log in on your website. Most users who use Facebook have their login stored so they will always have a session going whenever their browser is open.

    That would/is the best way to go about it as it doesn’t require you to ask the users for offline permissions which I suppose many users wouldn’t want to give. If for some reason you can’t use that approach I suggest getting offline permission and then storing a cookie on their computer with no timer on it so it won’t be automatically deleted.

    Sorry for the wall of text, but I hope I was somewhat helpful 🙂

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I would like to count the length of a string with PHP. The string
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I used javascript for loading a picture on my website depending on which small
this is what i have right now Drawing an RSS feed into the php,

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.