简体   繁体   中英

Passing the Facebook Authorization Token from PHP to Javascript

I'm trying to build an application on Facebook.com. It authenticates using PHP and stores the auth token in a variable, but then does a lot of client-side stuff using Javascript that requires the token.

I've looked through several other answers but can't seem to find one that applies to my problem. Basically, I'm trying to pass the PHP variable to Javascript using:

echo "<script>
var auth_token = '$auth_code';
</script>";

It seems simple enough, but when I try to pass an authorization token provided by Facebook from PHP to JS, it doesn't work. No value appears in the code, and it seems to throw strange errors on external pages:

GET http://0.49.channel.facebook.com/x/3638925121/3718215131/true/p_643080342=76 undefined (undefined)

Is there a way to do this easily? Or will I have to authenticate using Javascript?

Thanks!

You can store the auth token in a cookie.

You can set the cookie on the PHP side, and read it from javascript.

PHP:

setcookie()

http://php.net/manual/en/function.setcookie.php

Javascript:

document.cookie

http://www.quirksmode.org/js/cookies.html

I usually echo out the variable into a javascript variable string with quotes....as long as the string doesn't contain whatever quotes you are using or the quotes are escaped.

 var auth_token = '<?= $auth_token ?>'; 

This works for a lot of different things...just make sure your variable has something to echo out.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM