Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I've come accross the same problem lately, it seems that if after 5mins or so of inactivity you do a reload, the session does not persist. Sometimes it works, sometimes it doesn't. </p> <p>I've been looking into it for about the last week, and the only solution I could think of was to use the JS SDK to do a page reload with:</p> <pre><code>FB.Event.subscribe('auth.login', function(response) { window.location.reload(); }); </code></pre> <p>But I agree, it's not a very elegant solution in terms of UX. You should pass the <code>cookie</code> param in the PHP SDK, and the oauth param in JS SDK, and see if that works (it didn't for me): </p> <pre><code>$facebook = new Facebook(array( 'appId' =&gt; $config['facebook']['id'], 'secret' =&gt; $config['facebook']['secret'], 'cookie' =&gt; true )); </code></pre> <p><strong>AND</strong></p> <pre><code>FB.init({ appId : appId, channelUrl : '//domain.com/signon/channel.php', status : true, cookie : true, xfbml : true, oauth : true }); </code></pre> <p>Stranger even still, I re-downloaded the <a href="http://github.com/facebook/facebook-php-sdk" rel="nofollow">latest PHP SDK</a> from Github and uploaded it to a sandbox environment (Apache, PHP 5.4). I ran the example (with the JS SDK) AS-IS and it has the same issue!</p> <p>Now if the above just doesn't cut the mustard, I have some more suggestions. </p> <p><strong>CHANGING THE SDK UP A BIT</strong></p> <p>Firstly, passing</p> <p><code>$facebook-&gt;getAccessToken();</code></p> <p>will do you no good if <code>$user</code> returns 0. However, after doing a bit of digging around in <code>base_facebook.php</code>, I noticed that the method <code>getCode()</code> actually uses <code>$_REQUEST</code> to retrieve the authorization code from the query parameters. </p> <p>from the PHP Manual, $_REQUEST is</p> <blockquote> <p>An associative array that by default contains the contents of $_GET, $_POST and $_COOKIE.</p> </blockquote> <p><strong>BUT</strong></p> <p>It is very different to $_GET,$_POST or $_COOKIE. </p> <p>This can be a bit of a bugger depending on your setup. So instead, find the function <code>getCode()</code> in <code>base_facebook.php</code> that looks like this:</p> <pre><code>protected function getCode() { if (isset($_REQUEST['code'])) { if ($this-&gt;state !== null &amp;&amp; isset($_REQUEST['state']) &amp;&amp; $this-&gt;state === $_REQUEST['state']) { // CSRF state has done its job, so clear it $this-&gt;state = null; $this-&gt;clearPersistentData('state'); return $_REQUEST['code']; } else { self::errorLog('CSRF state token does not match one provided.'); return false; } } return false; } </code></pre> <p>and merge the query into a new array/variable (let's call it <code>$code_array</code>) to include the <code>$_GET</code>, <code>$_POST</code> &amp; <code>$_COOKIE</code> arrays using <a href="http://php.net/array_merge" rel="nofollow"><code>array_merge()</code></a> like so:</p> <pre><code>$code_array = array_merge($_GET,$_POST,$_COOKIE); </code></pre> <p>What you end up with is an array that contains all the data from the respective requests. The just go ahead and replace <code>$_REQUEST</code> with <code>$code_array</code> inside the function, i.e</p> <pre><code>\\replace $_REQUEST with $code_array; if (isset($code_array['code'])) { if ($this-&gt;state !== null &amp;&amp; isset($code_array['state']) &amp;&amp; $this-&gt;state === $code_array['state']) { //...and so on for the rest </code></pre> <p>This should do the job nicely (hopefully).</p> <p><strong>OPTIONAL - Extending your Access Token Lifetime</strong></p> <p>This is optional, but I included it anyway. Most New Apps will already have long lived Access Tokens, but just in case, you can use the <code>$facebook-&gt;setExtendedAccessToken();</code> method to transform your existing access token. </p> <p><strong>Note:</strong> You have to call <code>$facebook-&gt;setExtendedAccessToken();</code> before you actually get your access token with the <code>getAccessToken()</code> method.</p> <p>Now, using your code you will have </p> <pre><code>$user = $facebook-&gt;getUser(); if($user) { try { // Just to be sure, add access token to each request $facebook-&gt;setExtendedAccessToken(); $access_token = $facebook-&gt;getAccessToken(); // Fetch user details $user = $facebook-&gt;api('/me?access_token='.$access_token); } //and so on.. </code></pre> <p><strong>CONCLUSION</strong></p> <p>Breaking $_REQUEST into $_GET,$_POST &amp; $_COOKIE (Even though it includes all three by default) seems to ensure we can fetch the cookie that was set by the SDK(s) without much trouble. I say seems because heck, I'm not really 100% sure myself. </p> <p>I Used the above methods to get it working in my case, so I'm hoping to share some knowledge, as I've been losing far too much sleep over this problem, and couldn't find a viable solution.</p> <p>Hope this helps!</p> <p><strong>EDIT:</strong> I forgot to mention, changing the API request from </p> <pre><code>$user_profile = $facebook-&gt;api('/me'); </code></pre> <p>to</p> <pre><code>$user_profile = $facebook-&gt;api('/'.$user.'?access_token='.$access_token); </code></pre> <p>was something I also did, and it made a difference. :)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload