Skip to main content
2Charlie
Inspiring
February 9, 2016
Answered

How to capture values from referrer?

  • February 9, 2016
  • 1 reply
  • 3491 views

Is there a way to capture the value passed in the referrer? I need to capture that and do some searching.

    This topic has been closed for replies.
    Correct answer 2Charlie

    Carl,

    I'm trying out the function you posted above and for some reason it's returning blank. Below is my code:

    <cfhttp url="https://app.knowledgeowl.com/api/head/article.json" method="get" timeout="15">

      <cfhttpparam type="url" name="_authbykey" value="56a7d8c123131c4058361789">

        <cfhttpparam type="url" name="project_id" value="55c4ffd456131c527e7899fe6">

        <!---<cfhttpparam type="url" name="url_hash" value="calendar">--->

        

         <!--- Pass along any URL values. --->

        

         <cfhttpparam type="url" name="url_hash" value="#queryStringGetVar(CGI.http_referer, 'slug')#" />

         <cflog text="URL-Hash: #queryStringGetVar(CGI.http_referer, 'slug')#" type="Information" file="CGIparameters">

      

    </cfhttp>

    <cfscript>

      /**

    * Gets the value of a variable in a query string.

    *

    * @param query_string The query string to examine. (Required)

    * @param this_var_name The variable to look for. (Required)

    * @return Returns a string.

    * @author Shawn Seley (shawnse@hotmail.com)

    * @version 1, August 1, 2005

    */

      function queryStringGetVar(querystring, this_var_name){

      var re_found_struct = "";

      querystring = trim(querystring);

      re_found_struct = REFindNoCase("(^|[\?|&])#this_var_name#=([^\&]*)", querystring, 1, "true");

      // = re_found_struct.len & re_found_struct.pos

      if(arrayLen(re_found_struct.pos) gte 3) {

      if (re_found_struct.pos[3] GT 0) return urlDecode(mid(querystring, re_found_struct.pos[3], re_found_struct.len[3]));

      else return "";

      } else return "";

      }

    </cfscript>

    The log shows blank. So, I'm not quite sure why. The CGI.http_referrer shows this: https://devbox.mysite.com/kb/?slug=calendar


    Okay, so all I have to do to get the URL parameter value is URL.variable name.

    <cfhttp url="https://app.knowledgeowl.com/api/head/article.json" method="get" timeout="15">

      <cfhttpparam type="url" name="_authbykey" value="56a7d8c532131c4058362589">

        <cfhttpparam type="url" name="project_id" value="55c4ffd258561c527e294fe6">

        <!---<cfhttpparam type="url" name="url_hash" value="calendar">--->

        

         <!--- Pass along any URL values. --->

         <cfhttpparam type="url" name="url_hash" value="#URL.slug#" />

         <cflog text="URL-Hash: #URL.slug#" type="Information" file="CGIparameters">

      

    </cfhttp>

    1 reply

    Carl Von Stetten
    Brainiac
    February 9, 2016

    Can you elaborate a what "value" you are talking about?  Please be specific.

    2Charlie
    2CharlieAuthor
    Inspiring
    February 9, 2016

    I guess I can use <cfdump var="#CGI#"> to show and then get my values from the QUERY_STRING.

    Carl Von Stetten
    Brainiac
    February 9, 2016

    Possibly.  But keep in mind that dumping the CGI scope won't necessarily reveal every attribute available. Depending on the web server you are using, it may populate CGI variables that don't show in CFDUMP - you just have to know what they are and access them directly (as CGI.some_obscure_variable).

    What referrer information are you looking for?  From my local CF10 development environment, it looks like the CGI.HTTP_REFERER variable contains the full URL (including query string) from the referring page.  Is that what you were looking for?