02.18.2010
I have been racking my brain for some time on how to accomplish cross domain sessions. It was especially puzzling at first since the application I was working on ran multiple sites. So, the code base was the same, even though the domain might change.
I will not go too deep into security on this, I am just going to give you the tools to explore the concept.
The first step you will have to take is to create a services file on the original site providing the session, in this case I will be using rails.
-
#Controller
-
class ServiceController < ApplicationController
-
def sessioninfo
-
if session[:user]
-
@referrer = ‘var referrer=\’‘ + request.env[‘HTTP_REFERER’].to_s + ‘\’;’
-
@user = ‘var user=’ + session[:user].to_json + ‘;’
-
else
-
#there was no session
-
end
-
end
-
end
What ultimately gets outputted from this is the following:
-
#output
-
var referrer=‘http://www.thereferrer.com’;
-
var user={"user"{"username":"joshmattvander"}}
Now the session is ultimately exposed on the original site. But we need to get it from another site. The only way to accomplish that I can find is to use javascript. What you are going to do is add a script to the head of the document with your exposed service URL in my case (http://mysite.com/service/sessioninfo). The reason we are doing this in javascript, is because if we did this on the server side, the session would be blank. By doing this in javascript it is as if the user was returning to the URL themselves so the session is still alive and kicking.
-
-
$(document).ready(function(){
-
var scriptEl = $(‘<script type="text/javascript" src="http://mysite.com/service/sessioninfo"></script>’);
-
$(scriptEl).appendTo($(‘head’));
-
setTimeout(function(){
-
try {
-
if (referrer && user) {
-
alert(‘The referring site is: ‘ + referrer + ‘.’)
-
alert(‘The logged in user is: ‘ + user[‘user’].username + ‘.’);
-
}
-
} catch (e) {
-
alert("The session did not transfer")
-
}
-
}, 500);
-
});
-
This is the basic mechanism that allows this to happen, but the next step would be securing with some sort of key and adapting this to your needs.
As stated earlier - on my app there are multiple domains being run by a single application. So I can save the a generated key to a table, and then use that key to re-connect with ajax.
Posted in Ajax, Javascript, Rails No Comments »
09.04.2009
This tutorial is intended to be a first look at the concept of “Ajaxifying” your website. That is, creating a progressive-enhancement to how users navigate from page to page. Traditionally when a user clicks a link, the new page in it’s entirety will be loaded. If your header / navigation / footer stays the same throughout, there is little need to re-download that content, furthermore to re-render that content after it downloaded.
This is a generic pattern that you can expand on and implement with your web application.
First the Javascript
-
-
var QuickLink = Class.create({
-
-
currentPage : null,
-
-
initialize : function() {
-
this.attachLinkEvents();
-
//Set an interval to check for changes in the URI
-
setInterval(function(){
-
this.refresh();
-
}.bind(this), 1000);
-
},
-
-
refresh : function() {
-
//If a change has taken place, call the dispatch function
-
var url = window.location.hash.split(‘#’);
-
if (url.length > 1) {
-
this.dispatch(url[1]);
-
}
-
},
-
-
attachLinkEvents : function() {
-
//Gather a list of links, and replace the default action
-
//With our javascript action
-
var links = $$(‘a’);
-
links.each(function(linkElement, index) {
-
var href = linkElement.href;
-
if(href.indexOf(‘http://www.yourdomain.com/’) != -1) {
-
var uri = href.split(‘http://www.yourdomain.com’)[1];
-
Event.observe(linkElement, ‘click’, function(e){
-
window.location.hash = uri;
-
//Disallow the default click to change pages
-
Event.stop(e);
-
});
-
}
-
-
});
-
},
-
-
dispatch : function(url) {
-
//Check to see if the page you are linking to is NOT the current URL
-
//If different, then send your request
-
if (this.currentPage != url) {
-
this.currentPage = url;
-
var myAjax = new Ajax.Updater(‘content’, url, {
-
parameters : { ‘quick’ : true }
-
});
-
}
-
-
-
}
-
});
-
-
//initialize
-
-
new QuickLink();
-
-
Your template
-
-
<? if ($_POST[‘quick’]!=true) { ?>
-
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
-
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
-
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
-
<head>
-
<meta http-equiv="Content-type" content="text/html; charset=utf-8">
-
<title>Page Title</title>
-
</head>
-
<body>
-
<div id="content">
-
<? } ?>
-
Some content here
-
<? if ($_POST[‘quick’]!=true) { ?>
-
</div>
-
</body>
-
</html>
-
<? } ?>
-
In conclusion
What we are doing is using AJAX just to refresh the content area. Your template checks to see if it was an AJAX request and if so, does not send the header and footer of your content back. This saves you in the following areas:
- Does not have to re-download everything
- If you have scripts that need to be initialized, the scripts will not need to be reparsed
Moving further
If using a more advanced setup like an MVC framework or if your app has different scripts or elements depending on the page I suggest on the server side you instead send back a payload including dependent scripts and maybe flags to highlight elements as well. Then you can use output buffering to store the HTML chunk in part of the payload. Return the response as a JSON object have your Javascript put things where they need to go and THEN write the innerHTML of the content part of the payload.
Posted in Ajax, Performance No Comments »