In the Webform queue we regularly have requests to have a token that provides the value of the $_SERVER['HTTP_REFERER'] value. We used to have this ability before we switched to the official token system, but now there doesn't seem to be an equivalent using the normal token system. I couldn't find any modules that provide this token either, and given it's general-purpose nature I though it might be a good candidate for including directly in the the token.module.

Comments

ac’s picture

Huge +1 for this

Vacilando’s picture

https://drupal.org/project/google_analytics_referrer provides a tag that displays a list of referrers for the given page. If just one referrer is needed, for the given page, the syntax is [gar||1].

If there is interest, a real token could be added — just add an request in the module's issue queue.

[EDIT] On a second thought, I realize that for the given use case the referer needs to be available immediately while the fetching from Google Analytics (in the GAR module) takes time.

liquidcms’s picture

Issue summary: View changes

somewhat related.. i posted this a while back: https://www.drupal.org/node/2010898#comment-9231971

dehacker’s picture

Support for php variables %server[HTTP_REFERER] and %server[HTTP_USER_AGENT] were extremely useful in webform 3.

Some of our systems using Webform 3 can not be simply upgraded to Webform 4 because they rely on these variables for workflow in both helpdesk forms and applicant tracking. Webform 4 is dependent on the Token module to accomplish what server variables used to. As Webform is one of the most popular contrib modules it would be more robust if functionality were supported across Drupal 7 when updates are made to major modules with dependencies. Please add tokens for these server variables.

Chewits’s picture

I'm seeking for replacement of %server[HTTP_REFERER] in Webform too..

helmo’s picture

Here's a bit of code to add such tokens.... These two hooks can be added to your own custom module by replacing 'mytoken_custom' with your module name.

/**
 * Implements hook_token_info().
 *
 * @ingroup mytoken_custom
 */
function mytoken_custom_token_info() {
  $info = array();

  $info['types']['server'] = array(
    'name' => t('Server vars'),
    'description' => t('Tokens from _SERVER.'),
  );

  $info['tokens']['server']['HTTP_REFERER'] = array(
    'name' => t('HTTP_REFERER'),
    'description' => t("Var from _SERVER"),
  );

  $info['tokens']['server']['HTTP_USER_AGENT'] = array(
    'name' => t('HTTP_USER_AGENT'),
    'description' => t("Var from _SERVER"),
  );

  return $info;
}

/**
 * Implements hook_tokens().
 *
 * @ingroup mytoken_custom
 */
function mytoken_custom_tokens($type, $tokens, array $data = array(), array $options = array()) {
  $replacements = array();
  $sanitize = !empty($options['sanitize']);

  // Text format tokens.
  if ($type == 'server') {

    foreach ($tokens as $name => $original) {
      switch ($name) {
        case 'HTTP_REFERER':
          $replacements[$original] = $_SERVER['HTTP_REFERER'];
          if ($sanitize) {
            $replacements[$original] = filter_xss($replacements[$original]);
          }
          break;

        case 'HTTP_USER_AGENT':
          $replacements[$original] = $_SERVER['HTTP_USER_AGENT'];
          if ($sanitize) {
            $replacements[$original] = filter_xss($replacements[$original]);
          }
          break;
      }
    }
  }

  return $replacements;
}

jiv_e’s picture

Status: Active » Closed (won't fix)

I believe this solves the issue. Please reopen if you feel this is not the case!

bisonbleu’s picture

@helmo, once the suggested code in #6 is implemented in a custom module, how can I access these new tokens in e.g. Rules or elsewhere ?

helmo’s picture

That would be [server:HTTP_REFERER] and [server:HTTP_USER_AGENT]

tremor’s picture

I have tried this from #6 but it doesn't seem to be working.. is there any reason why it's just returning nothing?

mitchtollerud’s picture

#6 works for me in webform results. Thanks for the solution.

Yuri’s picture

#6 is working fine! Thanks!

Hitby’s picture

Hi, I've made a custom module referer_token containing the code above with the functions renamed

function referer_token_token_info() {

and

function referer_token_tokens($type, $tokens, array $data = array(), array $options = array()) {

then placed [server:HTTP_REFERER] in the redirect field on the webform. When I complete the form though I'm just left on the form page. Will this code work with webform-7.x-4.16?

As an aside, my use case is using Webform Private File Gateway to forward links on a page to the webform to collect an email address. The user then receives an email with their download link and I'd like them to be redirected back to the page on which they clicked the download link.

Cheers!

kristindev’s picture

Another up vote for #6

wlofgren’s picture

I've made a custom module that adds the referrer url to be able to use in Rules.

page_referrer.info

name = Rules Page Referrer
description = "Provides a page referrer for rules to use."
core = 7.x
package = Rules

page_referrer.module

function page_referrer_entity_property_info_alter(&$info) {
 
  $info['site']['properties']['current_page']['property info']['referrer'] = array(
    'label' => t("Page Referrer"),
    'description' => t("Information related to the current page's referrer request."),
    'getter callback' => 'entity_metadata_system_get_properties',
    'type' => 'struct',
    'property info' => array(
      'url' => array(
        'label' => t("URL"),
        'description' => t("The full URL of the referrer's page request."),
        'getter callback' => 'page_referrer_get_page_properties',
        'type' => 'uri',
      ),
    ),
  );
  
}


function page_referrer_get_page_properties($data = array(), array $options, $name) {
  switch ($name) {
    case 'url':
      return $_SERVER['HTTP_REFERER'];
  }
}
pmoz’s picture

Just in case anyone else stumbles upon this.

Add and enable the custom module that @helmo was kind enough to share in #6.

  1. Once it's enabled, on the webform add a text field with "Referring Page" for the label and a default value of [server:HTTP_REFERER] and set the field to disabled.
  2. Then under Form Settings, under Redirection location, Custom URL: enter
  3. [submission:values:referring_page:nolabel] and hit save.

Now when you view the form you should see the referring page in the disabled text filed. Disabling the field instead of hiding it makes it a bit easier to troubleshoot in the future.

You'll need to go through both steps for every form that you want to apply it to.

Overall I agree that this should be a part of webform but at least there is a workaround.

cguidog’s picture

Hi @pmoz,

I followed your instructions and I am halfway there.

My form is located in an url that looks like this: https://example.com/category/article

After adding the module this is what I'm getting: https://example.com/category/
So basically the article slug is not being included.

Any ideas what could be missing?

Thanks,

jamesfk’s picture

Hi everyone,

Thanks, @helmo for the code, which worked perfectly.

We needed this functionality for a couple of clients, so made a quick module that might be easier for anyone else needing this:

https://www.drupal.org/project/referrer_token

All the best,
James