About every sitelocking script I’ve found on the internet lacks the ability to handle subdomains which obviously is a very bad thing, since your sponsor might host his/her swf-files on a subdomain. I personally ran into this problem and figured I needed a solution…


Thanks to some guys at Mochi Forums a solution was formed.

The problem

Most sitelocking script handles urls as follows. If url = “http://www.subdomain.goplaysushi.com/” then script returns -> “subdomain.goplaysushi.com”. Of course you can change your if-clause to handle each specific sub-domain when selling each unique site-lock. But that’s of course not a very general solution. Instead we can solve it by doing something like the following..

1) Store allowed domains, load URL and remove “http://” plus possible slashes

var okDomains = ["blog.goplaysushi.com", "yoursite.com"];
var url:String = _url;
trace("Domain name pre string functions: "+url);

// Where does domain name begin
var urlStart:Number = url.indexOf("://")+3;

// Where does URL end
var urlEnd:Number = url.indexOf("/", urlStart);

// Substring domain name
var domain:String = url.substring(urlStart, urlEnd);

// Print result
trace("Domain name post string functions: "+domain);

2) Remove subdomains

// Where does domain ending begin
var lastDot:Number = domain.lastIndexOf(".")-1;

// Where does domain ending end
var domStart:Number = domain.lastIndexOf(".", lastDot)+1;

// Substring domain name to remove subdomains
domain = domain.substring(domStart, domain.length);

// Print final result
trace("Stripped domain is: "+ domain);

3) Perform domain check

// Pre-set allowance to false
var allowed = false;

// Iterate through allowed domains
for (i=0; i<okDomains.length; i++) {

  // If the stripped domain exists in allowed domains
  if (domain.indexOf(okDomains[i]) != -1) {
    allowed = true;
  }
}

// If allowance is false
if (allowed == false) {
  _root._alpha = 0;
  stop();
}

Note

This code was written by (and in discussion) by guys over at Mochi Ads community. I have modified and commented the code. Thanks to the guys who posted!

Please let me know if you find errors or have comments or ideas for improvement.

The full code

// Specify allowed domains
var okDomains = ["blog.goplaysushi.com", "yoursite.com"];
var url:String = _url;
trace("Domain name pre string functions: "+url);

// Where does domain name begin
var urlStart:Number = url.indexOf("://")+3;

// Where does URL end
var urlEnd:Number = url.indexOf("/", urlStart);

// Substring domain name
var domain:String = url.substring(urlStart, urlEnd);

// Print result
trace("Domain name post string functions: "+domain);

// Where does domain ending begin
var lastDot:Number = domain.lastIndexOf(".")-1;

// Where does domain ending end
var domStart:Number = domain.lastIndexOf(".", lastDot)+1;

// Substring domain name to remove subdomains
domain = domain.substring(domStart, domain.length);

// Print final result
trace("Stripped domain is: "+ domain);

// Where does domain ending begin
var lastDot:Number = domain.lastIndexOf(".")-1;

// Where does domain ending end
var domStart:Number = domain.lastIndexOf(".", lastDot)+1;

// Substring domain name to remove subdomains
domain = domain.substring(domStart, domain.length);

// Print final result
trace("Stripped domain is: "+ domain);

// Pre-set allowance to false
var allowed = false;

// Iterate through allowed domains
for (i=0; i<okDomains.length; i++) {

  // If the stripped domain exists in allowed domains
  if (domain.indexOf(okDomains[i]) != -1) {
    allowed = true;
  }
}

// If allowance is false
if (allowed == false) {
  _root._alpha = 0;
  stop();
}

Leave a Reply