Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var INFO = {
- PHP_SESSION_NAME: "PHPSESSID",
- SAFARI_AUTH_URL: "/safari.php",
- APP_ID: "your app id"
- };
- $(function () {
- initFacebook(function () {
- var authResponse = FB.getAuthResponse();
- var fields = [];
- var cookieName = "fbsr_" + INFO.APP_ID;
- fields['session_id'] = cookie(INFO.PHP_SESSION_NAME);
- fields['auth_response'] = authResponse || 0;
- fields['app_id'] = INFO.APP_ID;
- $.post(
- INFO.SAFARI_AUTH_URL,
- $.param(fields),
- function (response) {
- // make sure the cookie has been set
- var sessionId = cookie(INFO.PHP_SESSION_NAME);
- console.log(sessionId);
- var authResponse = cookie(cookieName);
- console.log(authResponse);
- },
- 'json'
- );
- });
- });
- /**
- * Gets, sets or a deletes a cookie
- * @param name
- * The name of the cookie
- * @param value
- * Optional. If passed, this is the new value of the cookie.
- * If null is passed here, the cookie is "deleted".
- * @param options
- * Optional hash of options, including:
- * "expires": number of milliseconds until expiration. Defaults to session cookie.
- * "domain": the domain to set cookie
- * "path": path to set cookie. Defaults to location.pathname
- * @return String
- * If only name was passed, returns the stored value of the cookie, or null.
- */
- function cookie(name, value, options) {
- if (typeof value != 'undefined') {
- var path, parts, domain = '';
- parts = Q.info.baseUrl.split('://');
- if (options && ('path' in options)) {
- path = ';path='+options.path;
- } else {
- path = ';path=/' + parts[1].split('/').slice(1).join('/');
- }
- if (options && ('domain' in options)) {
- domain = ';domain='+options.domain;
- } else {
- domain = ';domain=.' + parts[1].split('/').slice(0, 1);
- }
- if (value === null) {
- document.cookie = encodeURIComponent(name)+'=;expires=Thu, 01-Jan-1970 00:00:01 GMT'+path+domain;
- return null;
- }
- var expires = '';
- if (options && options.expires) {
- expires = new Date();
- expires.setTime((new Date()).getTime() + options.expires);
- expires = ';expires='+expires.toGMTString();
- }
- document.cookie = encodeURIComponent(name)+'='+encodeURIComponent(value)+expires+path+domain;
- return null;
- }
- // Otherwise, return the value
- var cookies = document.cookie.split(';');
- var result;
- for (var i=0; i<cookies.length; ++i) {
- parts = cookies[i].split('=');
- result = parts.splice(0, 1);
- result.push(parts.join('='));
- if (decodeURIComponent(result[0].trim()) === name) {
- return result.length < 2 ? null : decodeURIComponent(result[1]);
- }
- }
- return null;
- };
- function initFacebook(callback) {
- // should be only called once per app
- if (initFacebook.completed[Q.app]) {
- callback();
- return;
- }
- if (!$('#fb-root').length) {
- $('body').append($('<div id="fb-root" />'));
- }
- Q.addScript((window.location.protocol || 'http') + '//connect.facebook.net/en_US/all.js', function () {
- if (!initFacebook.completed[Q.app]) {
- FB.init(Q.extend({
- appId: INFO.APP_ID,
- status: true,
- cookie: true,
- oauth: true,
- xfbml: true
- }));
- }
- initFacebook.completed[Q.app] = true;
- if (callback) {
- callback();
- }
- });
- };
- initFacebook.completed = {};
Add Comment
Please, Sign In to add comment