• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 268
  • Last Modified:

looking to catch the inside and outside link referer in form

what is the best way in CF to get these 2 variables?

Inside link is the last link in the site before filling in the form. eg www.mysite.com/page25.cfm
<input type="Hidden" name="InsideLiink" value="#not sure what would go here#
OutsideLink is the link that brought them to the site. eg for a car company
http://www.google.com/search?hl=en&q=cars&aq=f&oq=
<input type="Hidden" name="OutsideLink" value="#not sure what would go here#

if the link comes from a search engine like google I'd like to parse it into 2 fileds like
SearchEngine = google.com
SearchKeywords = cars
...this last part we could leave for another question if you think it's a little cheeky for one question :)
0
Shawn
Asked:
Shawn
  • 15
  • 13
4 Solutions
 
ShawnAuthor Commented:
found the InsideLink...
<cfoutput>#HTTP_REFERER#</cfoutput> <-- gives previous page
0
 
ShawnAuthor Commented:
found the beginning of Outside link. I put this in the application.cfm
      <cfif #HTTP_REFERER# DOES NOT CONTAIN "127.0.0.1" and #HTTP_REFERER# DOES NOT CONTAIN "mysite.com">
      <cfset application.OutsideLink = #HTTP_REFERER#>
      </cfif>

I can see one problem with this...if in the referer link the mysite.com exists then it won't work. I'm sure there's a better way.

feel free to step in any time ;-)
0
 
gdemariaCommented:
Seems you have figured out that there is really only one referrer.   So to determine whether it's "inside" or "outside" you need to add a little logic.

It seems the places to get the "outside" referrer is the application.cfm file.   They may not be going directly to your Form page.

<cfif NOT isDefined("session.theReferrer")>
    <cfset session.theReferrer = cgi.HTTP_REFERER>
</cfif>

! please be sure to use the cgi. scope on the http_referer variable

Now for the "inside" referrer, you are looking for the page that takes the user to your Form page, right?
So, on your form page, you can have this...

<cfparam name="form.myReferrer" default="#cgi.http_referer#">

Then, to ensure it gets submitted with the form, you can add it as a hidden field...


<input type="hidden" name="myReferrer" value="#form.myReferrer#">

 
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
ShawnAuthor Commented:
hi gdemaria:,
I prefer your <cfif NOT isDefined("session.theReferrer")> method. thanks for the pointer.

Is there a difference between the cf http_referer and the cgi.http_referer?
0
 
gdemariaCommented:
not sure what you are referring to by  "cf http_referer"

Coldfusion has a seriers of CGI scoped variables with system information such as the domain, the query string the IP address etc.

This one gives you the referrer...

CGI.HTTP_REFERER

In fact, all variables should be scoped to reduce bugs and improve performance.  But that's another debate :)
0
 
ShawnAuthor Commented:
>>not sure what you are referring to by  "cf http_referer"

in coldfusion I don't need to add "cgi." as shown above there is no cgi and it "seems" to give the same result
eg <cfoutput>#HTTP_REFERER#</cfoutput>  <-- No mention of cgi here

is there a difference?
0
 
ShawnAuthor Commented:
the next part I need is to parse the OutsideLink. I've started it below...
<cfset OutsideLink = "http://www.google.com/search?hl=en&q=cars&aq=f&oq=">
<cfset outHTTP = Find("http://", OutsideLink)>
<cfset outHTTPS = Find("https://", OutsideLink)>
<cfset outWWW = Find("www", OutsideLink)>
<cfset OutsideDomain = ""> <!--- Should get domain whether 2 or 3 letters eg. .com or .ca --->
<cfset OutsideQuery = ""> <!--- Should get anything after domain - the "/" --->
 
<!--- Then do some sort of a replace to get OutsideDomain OutsideQuery --->
 
<cfoutput><p>
OutsideLink #OutsideLink#<br>
OutsideDomain #OutsideDomain#<br>
OutsideQuery #OutsideQuery#<br>
<br>
</p></cfoutput>

Open in new window

0
 
gdemariaCommented:
What is your objective with the parsing?   You just want to get one part of it or are you trying to record every part of it... ?  (what end result are you after...)
0
 
gdemariaCommented:
> in coldfusion I don't need to add "cgi." as shown above there is no cgi and it "seems" to give the same result
>  eg <cfoutput>#HTTP_REFERER#</cfoutput>  <-- No mention of cgi here

>  is there a difference?



There is no difference.   Variables can be accessed without their scope..

#lastName#
#form.lastname#    
#theQuery.lastName#
#variables.lastName#


But the right way to refer to variables is with the scope, in all cases...



0
 
ShawnAuthor Commented:
when someone submits a form once we have the outside link we want to save the site in one field and the qury in the other field so we can internally build stats on where (the domain) and how (did they come form a specific page, what were the key words in the query, etc.)

so with the eg
http://www.google.com/search?hl=en&q=cars&aq=f&oq=
I want
OutsideDomain = google.com
OutsideQuery = search?hl=en&q=cars&aq=f&oq=
0
 
ShawnAuthor Commented:
>But the right way to refer to variables is with the scope, in all cases...
ok, thanks for the pointer. I'm pretty much self taught in cf so it's nice to know how I "should" be doing things
0
 
gdemariaCommented:
The list functions are really useful for parsing...  you can always count on the ? to be there for the query string...  


<cfset theReferrer = "http://www.google.com/search?hl=en&q=cars&aq=f&oq=">

<cfif listLen(theReferrer,"?") gt 1> <!--- there is a query string --->
  <cfset outQuery = listRest(theReferrer,"?")>
  <cfset outDomain = listFirst(theReferrer,"?")>
<cfelse>
  <cfset outQuery   = "">
  <cfset outDomain = theReferrer>
</cfif>
<cfif listLen(outDomain,"/") gt 1>
   <cfset outDomain = listGetAt(outDomain,2,"/")>
</cfif>
0
 
ShawnAuthor Commented:
this is ok if we are sure to have a ? in the link but often there isn't one. I like the list functions though. :-) they should do the trick
0
 
gdemariaCommented:
> in the link but often there isn't one

If there isn't a ? then the code goes to the <cfelse>  which handles no query string..

0
 
ShawnAuthor Commented:
in your example the results are

outDomain www.google.com
outQuery hl=en&q=cars&aq=f&oq=

whereas I am looking for
OutsideDomain = google.com
OutsideQuery = search?hl=en&q=cars&aq=f&oq=

I suppose we could get rid of the www. with a replace
it's the second one that soncerns me...I need the search?
0
 
gdemariaCommented:
it's easy enough to remove the first subdomain if you wish

<cfif listLen(outDomain,".") gt 2>
  <cfset outDomain = listRest(outDomain,".")>
</cfif>


Are you sure you want the /search to be on the other side, it's not part of the query string, not sure what that would tell you?

0
 
gdemariaCommented:
> I suppose we could get rid of the www. with a replace

it's not always going to be www.

It could be anything really, including not there    secure.theSite.com   search.theSite.com   etc..

It could even be ...     secure.newyork.usa.mySite.com
0
 
ShawnAuthor Commented:
ok, I'm fine with OutsideDomain...see below.

It's OutsideQuery I'm still working on. I need the /search on the Query rather than the domain as you did above.
<cfset theReferrer = "http://www.google.com/search/hl=en&q=cars&aq=f&oq=">
 
<cfif theReferrer contains "www.">
<cfset outDomain = Replace(listGetAt(theReferrer,2,"/"), "www.", "")>
<cfelse>
<cfset outDomain = listGetAt(theReferrer,2,"/")>
</cfif>

Open in new window

0
 
ShawnAuthor Commented:
>>It could be anything really, including not there    secure.theSite.com   search.theSite.com   etc..
that's ok. I'm fine with that. the examples you gave are different to me. www doesn't give any more ingormation. If it was " secure.theSite.com " however we would know it came from the secure subdomain. Possibly useful.
0
 
gdemariaCommented:
can should test for https://  to be secure, just as you did in your original example
The name "secure" does not ensure SSL...
0
 
gdemariaCommented:

I think this example you had may effect www. in the wrong place for example

 http://onLineWWW.com/

Perhaps this...

<cfset theReferrer = "http://www.google.com/search/hl=en&q=cars&aq=f&oq=">
 
<cfset outDomain = listGetAt(theReferrer,2,"/")>
<cfif left(outDomain,4) is "www.">
   <cfset outDomain = listRest(outDomain,".")>
</cfif>

Open in new window

0
 
gdemariaCommented:

Perhaps something like this to get the part after the domain...

<cfset outQuery = listRest(theReferrer,"/")>
<cfset outQuery = listRest(outQuery,"/")>
0
 
ShawnAuthor Commented:
I think I have it now. what do you think?
<cfset theReferrer = "https://google.com/search/hl=en&q=cars&aq=f&oq=">
 
<cfset outDomain = listGetAt(theReferrer,2,"/")>
	<cfif left(outDomain,4) is "www.">
   <cfset outDomain = listRest(outDomain,".")>
	</cfif>
 
<cfset outQuery = listRest(theReferrer,"/")>
<cfset outQuery = listRest(outQuery,"/")>
 
<cfoutput>
outDomain: #outDomain#<br>
outQuery: #outQuery#<br>
</cfoutput>

Open in new window

0
 
ShawnAuthor Commented:
ListGetAt update with empty string throws error...seems to be common. looking into it
0
 
gdemariaCommented:
looks good, the only problem is that if your referrer is empty, line 3 will throw an error because there is no element #2 for listGetAt()

So you need to use the CFIF

<cfif listLen(theReferrer,"/") gt 1>
  ......
0
 
gdemariaCommented:
ha, I had left me screen so I didn't see your post
0
 
ShawnAuthor Commented:
all works great now. Thanks for your help. :-)
Shawn
0
 
ShawnAuthor Commented:
final code in application.cfm

thanks again.
	<cfif NOT isDefined("application.OutsideLink")>
    <cfset application.OutsideLink = cgi.HTTP_REFERER>
	</cfif>	
	<cfif listLen(application.OutsideLink,"/") gt 1>
	<cfset application.OutsideDomain = listGetAt(application.OutsideLink,2,"/")>
		<cfif left(application.OutsideDomain,4) is "www.">
   		<cfset application.OutsideDomain = listRest(application.OutsideDomain,".")>
		</cfif>
	<cfset application.OutsideQuery = listRest(application.OutsideLink,"/")>
	<cfset application.OutsideQuery = listRest(application.OutsideQuery,"/")>
	<cfelse>
   	<cfset application.OutsideDomain = "">
	<cfset application.OutsideQuery = "">
	</cfif>

Open in new window

0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 15
  • 13
Tackle projects and never again get stuck behind a technical roadblock.
Join Now