CSRF state token does not match one provided FB PHP SDK 3.1.1 Oauth 2.0

后端 未结 9 2003
难免孤独
难免孤独 2020-12-01 12:12

My server logs show a \"CSRF state token does not match one provided\" error which seems to happen for almost every user. However, the users are created and/or authenticated

9条回答
  •  天涯浪人
    2020-12-01 12:47

    To add a bit to chesles's answer, this problem can occur if you're playing with the session_start() - session_write_close() functions, as I did.

    If there is no started session when you're requesting the loginUrl, you'll get this error.

    Sidenote: Why bother stopping the session?

    Scripts that use sessions stops each other, because they're waiting for the session array to be available to use.

    Imagine that you have a popular application, with thousands of users, and have an action (a php script) where you post a picture. Something like this:

    --starting session at the top of the script

    --connecting to facebook

    --creating the image

    --sharing the image with the api call

    --script end, session closes automatically

    Doing this, the session will be used by the script for a long time for no reason. Be careful with such scripts, use something like this instead:

    --starting session right before where you create the facebook object

    --connecting to facebook

    --closing session with session_write_close(), the session array's available, other scripts can load

    --creating the image

    --sharing the image with the api call /* It think this doesn't need a session. */

    --script end, session already closed manually.

    Cheers.

提交回复
热议问题