‘code’ Archive

Get a domain from a URL

7 October 2009 17:11

Been looking for a function in ColdFusion to get the root domain from a url. There are a few but none actually cope with the many combinations. This function by no means copes with every combination however it does cover quite a lot. Comments inline indicate which domains the rules handle.


<cffunction name="getRootDomain" returntype="string" output="no" hint="Take a URL and get the domain from it.">
<cfargument name="url" type="string" required="true">
<cfset var dots = listLen(arguments.url,".")>
</cfset><cfset var domain = "">
</cfset><cfset host = arguments.url>
<!--- host.com, host.biz, host.ly --->
<cfif dots eq 2>
<cfset domain = host>
</cfset></cfif>
<cfif dots eq 3>
<!--- www.host.com, sub.host.com, www.host.mobi, www.host.ly, del.icio.us --->
</cfif><cfif listfirst(host,".") eq "www" or
((len(listgetat(host,3,".")) eq 3 or len(listgetat(host,3,".")) eq 2) and len(listgetat(host,"2",".")) gte 3)>
<cfset domain = listdeleteat(host,1,".")>
</cfset></cfif>
<!--- host.co.uk, host.com.za --->
<cfif (len(listgetat(host,3,".")) eq 2 and (len(listgetat(host,2,".")) eq 2) or len(listgetat(host,2,".")) eq 3)>
<cfset domain = host>
</cfset></cfif>
<cfif dots eq 4>
<!--- www.host.ltd.uk, www.host.co.uk --->
</cfif><cfif len(listgetat(host,4,".")) eq 2 and (len(listgetat(host,3,".")) eq 2 or len(listgetat(host,3,".")) eq 3)>
<cfset domain = listdeleteat(host,1,".")>
</cfset></cfif>
<!--- www.sub.host.com, sub.sub.host.com --->
<cfif len(listgetat(host,4,".")) eq 3 and len(listgetat(host,4,".")) gte 3>
<cfset domain = listdeleteat(listdeleteat(host,1,"."),1,".")>
</cfset></cfif>
<cfif dots eq 5>
<!--- sub.sub.host.co.uk, www.sub.host.co.uk, www.sub.host.com.za --->
</cfif><cfif len(listgetat(host,5,".")) eq 2 and (len(listgetat(host,4,".")) eq 2 or len(listgetat(host,4,".")) eq 3)>
<cfset domain = listdeleteat(listdeleteat(host,1,"."),1,".")>
</cfset></cfif>
<cfreturn domain />
</cfset></cfargument></cffunction>

Filed: Technology // Tagged: , // 1 Comment

CFMail and Gmail

11 July 2008 12:20

Been looking a lot lately at various email related projects. One of the issues I noticed as part of this work is that if you send a mail with cfmail to a gmail account that only has a cfmailpart text/plain gmail won’t render it.

If you send both, it works fine.

Plain
Strong HTML

So, if you’re sending plain only, don’t put it in a cfmailpart. Probably shouldn’t have been in the first place, but, issue now solved.

Filed: Technology // Tagged: , , , // No Comments

CSS Fun

27 February 2008 9:40

What does a website look back to front? Now you can see, add this CSS to your site a reload. Entirely pointless but quite amusing.

* {
direction: rtl;
unicode-bidi: bidi-override;
}

Filed: Technology // Tagged: , // No Comments

HTML Tag List

19 January 2008 21:09

I need to strip our HTML tags, but not all of them, in ColdFusion so needed a CSV of HTML tags. Now, this is probably quite useless to most but someone but find it useful!

a,abbr,acronym,address,applet,area,b,base,basefont,bdo,big,blockquote,body,br,
button,caption,center,cite,code,col,colgroup,,dd,del,dfn,dir,div,dl,dt,em,fieldset,
font,form,frame,frameset,h1,h2,h3,h4,h5,h6,head,hr,html,i,iframe,img,input,ins,
isinde,kbd,label,legend,li,link,map,menu,meta,noframes,noscript,object,ol,
optgroup,option,p,param,pre,q,s,samp,script,select,small,span,strike,strong,
style,sub,sup,table,tbody,td,textarea,tfoot,th,thead,title,tr,tt,u,ul,var

Filed: Technology // Tagged: // No Comments

Aptana 1.0

31 October 2007 15:21

I changed over to using Aptana from Eclipse a while ago now and haven’t looked back. It works fine with CFEclipse, subclipse and also has the benefit of built in CSS/JS support and optional AIR, PHP and Rails plugins.

Anyway, they’ve just released version 1 and also a slightly more feature filled “Pro” version.

Filed: Technology // Tagged: , // No Comments

Alternative to Captcha, Sort Of

11 October 2007 22:47

I’ve seen a lot of Captcha (Completely Automated Public Turing Test to Tell Computers and Humans Apart) graphics on sites now-a-days, some of which are quite hard to read.

One alternative, and don’t get me wrong it’s not better because it’s still text however it will make it a bit harder for bots to spam you, is to ask a question and have the user give the answer.

In this example I’ll ask the question:

<blockquote>What is two added with four? Please enter the answer as a number.</blockquote>

Below is a very basic ColdFusion page with that example.

<!--- store the numbers as words in a list, could also been and array --->
<cfset numberWords = "one,two,three,four,five,six,seven,eight,nine,ten">
<!--- get two numbers --->
<cfset n1 = randrange(1,10)>
<cfset n2 = randrange(1,10)>
<!--- calculate the total --->
<cfset nt = n1 + n2>
<!--- hash that value so it can't be seen in the source --->
<cfset nh = hash(nt)>
<cfoutput>
What is #listGetAt(numberWords,n1)# added to #listGetAt(numberWords,n2)#?
</cfoutput>
<form name="frm" method="post" action="captcha.cfm">
<!--- store the hash value in the form --->
<input type="hidden" name="hashvalue" value="<cfoutput>#nh#</cfoutput>">
<input type="text" name="answer">
<input type="submit">
</form>
<!--- hash the user's answer and compare to the passed hashed value --->
<cfif isDefined("form.answer")>
<cfif hash(form.answer) neq form.hashvalue>
<p>Nope</p>
<cfelse>
<p>Yay!</p>
</cfif>
</cfif>

Hopefully that might help someone!

Filed: Technology // Tagged: , , // No Comments

Prototype Validation using Ajax

5 June 2007 22:34

I’ve been using the excellent Prototype validation over at <a href=”http://tetlaw.id.au/view/javascript/really-easy-field-validation”>dexagogo</a> but recently wanted to have a a feature so as when you type a username in a sign up form it fires a real time lookup to check the username availability. After a bit of Googling and some experimentation I came up with the following. Hopefully it’ll save you some time. In this case the lookup is done via Coldfusion which encodes and returns JSON.

The form bit is pretty standard.

<div class="form-row">
<div class="field-label"><label for="field1">Username</label>:</div>
<div class="field-widget"><input name="field1" id="field1" class="text required validate-username" title="Enter yourusername." /></div>
</div>

The Javascript bit included on this page in an external JS file.

Validation.addAllThese([
['validate-username','Sorry, that username is already taken.',function(v,elm) {
if(elm._ajaxValidating && elm._hasAjaxValidateResult) {
elm._ajaxValidating = false;
elm._hasAjaxValidateResult = false;
return elm._ajaxValidateResult;
}
var sendRequest = function() {
new Ajax.Request('/path/to/check/file?username='+v+'&r='+Math.random(),{
onSuccess : function(response) {
elm._ajaxValidateResult = eval(response.responseText);
elm._hasAjaxValidateResult = true;
Validation.test('validate-username',elm);
}
});
elm._ajaxValidating = true;
return true;
}
return elm._ajaxValidating || Validation.get('IsEmpty').test(v) || sendRequest();
}]
]);

The Javascript bit in the page.

<script language="javascript" type="text/javascript">
var valid_register = new Validation('register', {immediate:true,stopOnFirst:true,onSubmit:false});
</script>

The form is submitted with an Event.observe on the submit button I have as well.

And so concludes my first geekish post for a while, it feels strangely good!

Filed: Technology // Tagged: , // 2 Comments

Comma’s Are Evil

13 March 2007 15:02

Aside from the fact that CSS cross browser is often quite a fun exercise and we all know how lovely MSIE can be in it’s quirks as indeed can Firefox, but, today I’ve leart that the comma is indeed evil.

The CSS file in question is quite large, a couple of hundred lines or so, in amungst it was a simple class:

#text p, { padding: 0; margin: 0; color: #ffffff; }

The issue was that the first paragraph tag in the text div had extra padding on the top whereas in IE it wasn’t there. The comma should be an obvious spot but within the larger file I’d overlooked it. Ah well… back to the CSS grindstone!

Filed: Technology // Tagged: // No Comments

XPath is my new friend

12 February 2007 22:29

Doing something at work which involves me grabbing an XML feed from a third party provider on a daily basis then sticking it in a database so it can be reused as the feed only updates at midnight GMT.

Quite an easy task, cfhttp the file, loop over it, insert to the database. Well, this was made even easier by me discovering the entirely not new XPath (or here). Another quick google found a tutorial and after a quick play using xmlParse() and xmlSearch() in Coldfusion 7 my feed was filtered and inserted for reuse. It’s been around for a while, but, if you’re using CF7 and working with XML and haven’t seen XPath I suggest you take a look.

Quite nice as well as I actually learned something today!

Filed: Technology // Tagged: , , , // No Comments

script.aculo.us 1.7.0

23 January 2007 12:26

A new version of script.aculo.us has now been released. It includes the final version of prototype (1.5.0) and a new Morph effect amongst other bits and bobs.

Filed: Technology // Tagged: , , , // No Comments

 
Twitter   •   About   •   Contact
©2017 Ian Winter. All Rights Reserved.   •   Powered by WordPress   •   Hosted at Memset