1

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!

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

Works great! Thanks a lot, you've helped me solve a day-long headache :)
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.