Originally created by: tatters1979
Hi, I've moved to the latest version of the SDK and tried the simple login example for starters and it was working fine a few days ago. However I now get the following error:
Facebook SDK returned an error: Cross-site request forgery validation failed. Required param "state" missing.
I can't figure out the problem, I've had a look at the following section of the Helper.
/**
* Validate the request against a cross-site request forgery.
*
* @throws FacebookSDKException
*/
protected function validateCsrf()
{
$state = $this->getState();
$savedState = $this->persistentDataHandler->get('state');
if (!$state || !$savedState) {
throw new FacebookSDKException('Cross-site request forgery validation failed. Required param "state" missing.');
}
$savedLen = strlen($savedState);
$givenLen = strlen($state);
if ($savedLen !== $givenLen) {
throw new FacebookSDKException('Cross-site request forgery validation failed. The "state" param from the URL and session do not match.');
}
$result = 0;
for ($i = 0; $i < $savedLen; $i++) {
$result |= ord($state[$i]) ^ ord($savedState[$i]);
}
if ($result !== 0) {
throw new FacebookSDKException('Cross-site request forgery validation failed. The "state" param from the URL and session do not match.');
}
}
View and moderate all "tickets Discussion" comments posted by this user
Mark all as spam, and block user from posting to "Tickets"
Originally posted by: SammyK
Can you provide the snippet of code that's causing the error?
View and moderate all "tickets Discussion" comments posted by this user
Mark all as spam, and block user from posting to "Tickets"
Originally posted by: tatters1979
This is the callback php from the example in the docs, thanks for the quick response:
View and moderate all "tickets Discussion" comments posted by this user
Mark all as spam, and block user from posting to "Tickets"
Originally posted by: SammyK
After the
catch(Facebook\Exceptions\FacebookResponseException $e)line can youvar_dump()any possible user errors?View and moderate all "tickets Discussion" comments posted by this user
Mark all as spam, and block user from posting to "Tickets"
Originally posted by: tatters1979
Changed to the following, no new errors?
View and moderate all "tickets Discussion" comments posted by this user
Mark all as spam, and block user from posting to "Tickets"
Originally posted by: SammyK
Sorry, I meant to paste that after the
catch(Facebook\Exceptions\FacebookSDKException $e)line. :) I'm curious if you'll seeNULLjust before the error message.Can you paste in the callback URL that Facebook is redirecting to with the value of the
code=param removed?View and moderate all "tickets Discussion" comments posted by this user
Mark all as spam, and block user from posting to "Tickets"
Originally posted by: tatters1979
Hi Sammy,
Thanks, I dropped that to the correct line and "NULL" was returned along with the original error.
I found a similar question on Stack Overflow and he said to solve this problem he changed the UTF-8 to UTF8 w/o BOM. Not sure how this affect the above but he said it worked on his code?
http://stackoverflow.com/questions/31520593/facebook-php-sdk-5-api-2-4-cross-site-request-forgery-validation-failed-r/31542061?noredirect=1#comment51055949_31542061
View and moderate all "tickets Discussion" comments posted by this user
Mark all as spam, and block user from posting to "Tickets"
Originally posted by: SammyK
Do you have display errors on? If not, for testing add these lines at the top of your script after the opening
<?phptag:If you had a BOM in your file you should see an error about not being able to start the session since headers have been sent. If you see that, you'll need to remove or ignore the BOM.
If that doesn't help, can you change the
var_dump()you added to this?Just want to make sure the proper handler is being used.
View and moderate all "tickets Discussion" comments posted by this user
Mark all as spam, and block user from posting to "Tickets"
Originally posted by: tatters1979
Added those changes and got a response:
object(Facebook\PersistentData\FacebookSessionPersistentDataHandler)#9 (1) { ["sessionPrefix":protected]=> string(6) "FBRLH_" }
Facebook SDK returned an error: Cross-site request forgery validation failed. Required param "state" missing.
View and moderate all "tickets Discussion" comments posted by this user
Mark all as spam, and block user from posting to "Tickets"
Originally posted by: SammyK
Are you doing any load-balancing on the server side? I curious why your sessions seem to not be working. How about var dumping your $_GET & $_SESSION. Do you see the
stateparam in either?View and moderate all "tickets Discussion" comments posted by this user
Mark all as spam, and block user from posting to "Tickets"
Originally posted by: tatters1979
My service provider is Network Solutions and sessions have always been fine (I use them for the Twitter API)?
returned the state param and the code param
returns nothing?
View and moderate all "tickets Discussion" comments posted by this user
Mark all as spam, and block user from posting to "Tickets"
Originally posted by: SammyK
I'm thinking this is related to [#470]. What do you guys think? cc @yguedidi @devmsh
Related
Tickets:
#470View and moderate all "tickets Discussion" comments posted by this user
Mark all as spam, and block user from posting to "Tickets"
Originally posted by: tatters1979
hmm it does sound like it...I take there's no work around available?
View and moderate all "tickets Discussion" comments posted by this user
Mark all as spam, and block user from posting to "Tickets"
Originally posted by: SammyK
Not yet. Got to narrow the bug down to what's causing people's sessions to reset on the callback. :/
View and moderate all "tickets Discussion" comments posted by this user
Mark all as spam, and block user from posting to "Tickets"
Originally posted by: tatters1979
No worries, I'll keep checking back for any updates....Thanks for your help SammyK.
View and moderate all "tickets Discussion" comments posted by this user
Mark all as spam, and block user from posting to "Tickets"
Originally posted by: SammyK
Np! Hopefully we'll be able to nip this one in the bud.
View and moderate all "tickets Discussion" comments posted by this user
Mark all as spam, and block user from posting to "Tickets"
Originally posted by: eduardomazolini
I've been with the same problem, I lost more than a day.
Maybe I've just done something stupid, but I walked into my site without putting "www" and put "www" in the callback URL.
The browser created two different cookies.
$currentCookieParams = session_get_cookie_params();
$rootDomain = '.example.com';
session_set_cookie_params(
$currentCookieParams["lifetime"],
$currentCookieParams["path"],
$rootDomain,
$currentCookieParams["secure"],
$currentCookieParams["httponly"]
);
session_start();
View and moderate all "tickets Discussion" comments posted by this user
Mark all as spam, and block user from posting to "Tickets"
Originally posted by: tatters1979
@SammyK Couple of developments after looking at the code.
I think the documentation on the Facebook pages might be out of date in a few places (examples).
I changed the graph version from v2.2 from v2.4 on the login.php and fb-callback.php files even though the facebook pages says 2.3 is the latest version?
I also added "www" to the call back URL on the login.php file and Oauth field in the App settings.
This seems to be working now? I'm not sure what did it? However the Metadata files are now throwing a fatal error/uncaught exception:
One problem solved,another one created.
View and moderate all "tickets Discussion" comments posted by this user
Mark all as spam, and block user from posting to "Tickets"
Originally posted by: tatters1979
@SammyK Problem solved, I removed the second section of the old code on the example asking for a long lived token and it works.
I also added the code for retreiving user details and for posting an image and both worked. So it looks like a few examples are out of date on the FB developer pages, this might trip a few noobs like myself up.
One more thing, does a POST from a form for images - $_FILES['image_file']['name']; work on the image upload? I had to include the full file path for an image I had in my root folder...instead of img/rest.jpg?
Thanks for your help.
View and moderate all "tickets Discussion" comments posted by this user
Mark all as spam, and block user from posting to "Tickets"
Originally posted by: SammyK
Yeah, I've been meaning to get in there and make some updates. I've also found a few type-o's.
Yeah, if you're using the "www" version to generate the "login link" and you get redirected to the non-www version, you'll run into issues with your session.
But there's still another bug going on with the sessions that's been quite illusive. :/
What was the code that was throwing the error? Were you trying to validate the app ID?
You can move your file upload to a temp location & then upload it.
Naturally you'd want to validate the upload and all that before blindly moving it around. :)
View and moderate all "tickets Discussion" comments posted by this user
Mark all as spam, and block user from posting to "Tickets"
Originally posted by: tatters1979
Yeah, I removed the second section of the code and it started working fine (see below).
However my callback doesn't validate (commented out) the access token or exchange it for a long lived one either. It would be nice to get this working but the error is as follows:
Notice: Undefined variable: config in /path/htdocs/fb-callbacks.php on line 68
and the error above re: AccessTokenMetadata.php
View and moderate all "tickets Discussion" comments posted by this user
Mark all as spam, and block user from posting to "Tickets"
Originally posted by: SammyK
You can delete all these line if you're not trying to validate the access token:
View and moderate all "tickets Discussion" comments posted by this user
Mark all as spam, and block user from posting to "Tickets"
Originally posted by: tatters1979
That removed the error but the following code echos nothing?
View and moderate all "tickets Discussion" comments posted by this user
Mark all as spam, and block user from posting to "Tickets"
Originally posted by: SammyK
Then it sounds like the access token you're getting is already long-lived. :)
View and moderate all "tickets Discussion" comments posted by this user
Mark all as spam, and block user from posting to "Tickets"
Originally posted by: tatters1979
That would explain it...Happy days...
Thanks for the tip on the file uploads...worked a treat. Just need to master batch uploads and I think I'm getting somewhere :)
View and moderate all "tickets Discussion" comments posted by this user
Mark all as spam, and block user from posting to "Tickets"
Originally posted by: tatters1979
@SammyK Hope I'm not being a pain. Batch uploads (4 images/photos) are working but it's posting the same picture (the last one) 4 times when I use the following. The comments are all different but the images should be different also?
The form is like so:
Many Thanks