Skip to main content
Inspiring
October 23, 2007
Question

UDF

  • October 23, 2007
  • 1 reply
  • 434 views
I am trying to implement my first udf http://www.cflib.org/udf.cfm?id=585&enable=1 which list the keywords received in a cgi.http_referer but I don´t understand it properly.

I have the udf on one page saved as a .cfm which is included on the Application.cfm (please see UDF code below)
My script which I put on the same page is here:
<cfset greferer= cgi.http_referer>
<cfoutput>
Keywords from #referer# = #GetGoogleKeywords(greferer)#<br>
</cfoutput>

How do I implement this rigth? Thank you
UDF:
<cfscript>/*
** Pass it a http_referer value and this fuction will parse out the keywords used to find it if referred from Google.
*
* @9397041 referer The referer string to check. (Required)
* @Return Returns a string.
* @7111211 Matthew Fusfield (&#109;&#97;&#116;&#116;&#64;&#102;&#117;&#115;&#46;&#110;&#101;&#116;)
* @version 1, June 28, 2002
*/
function getGoogleKeywords(referer) {

var Keywords='';
var StartPos=0;
var EndString='';

if (referer contains 'google.com') {
StartPos=ReFindNoCase('q=.',referer);

if (StartPos GT 0) {
EndString=mid(referer,StartPos+2,Len(referer));
Keywords=ReReplaceNoCase(EndString,'&.*','','ALL');
Keywords=URLDecode(Keywords);
}

return Keywords;
}
else {
return '';
}



}</cfscript>

This topic has been closed for replies.

1 reply

October 23, 2007
From your question, the following is what I would suggest, please post back if not suitable.

For ease, I would out the funciton in the request scope by having the following line directly after the UDF above:

request.getGoogleKeywords = getGoogleKeywords;

Then, on any page you want to use it, just have the following:

thisGoogleKeyWords = request.getGoogleKeyWords(CGI.HTTP_REFERRER);

This will then give you a list of the Google keywords that were used to access the page to do what you want with on that page (ie highlighting).

By including the CFM template that contains the UDF in the Application.cfm it will then be available to any other template via the request scope.

Couple of caveats:
1. Not exactly best Software Engineering practice, but lets get it working first.
2. CGI.HTTP_REFERRER is sometimes blank depending on the Web Server/Application Server/Browser so do _not_ make it essential that it is present
3. Above is rough, you need to give more information about what you want to actually do with the function if that doesn't give you a starter (you do know why you want to see the Google Keywords from the referrer, right?)
Inspiring
November 4, 2007
Thank you for the reply dacf I am confused as to where to put the cfscript:
this code:
request.getGoogleKeywords = getGoogleKeywords;

Then, on the page I want to use it how do I output this?
thisGoogleKeyWords = request.getGoogleKeyWords(CGI.HTTP_REFERRER);

TIA