2

Using Webform 7.x-3.18, I'd like to save the referring URL of an anonymous visitor on a webform. I added some code to MYTHEME__preprocess_page() in template.php to save the info as a cookie: user_cookie_save(array('webform_referrer' => $_SERVER['HTTP_REFERER']));

On my webform I added a hidden component, with a default value of %cookie[Drupal_visitor_webform_referrer].

I'm testing with safari/chrome/ff on a Mac and on Windows. I can get this to work on my local testserver, but only about 10% of the time on the production server, and never from Windows to the production server. The production server has hardware load balancing. I'm suspecting this is a caching issue. I've also read that server vars may not be available as webform default values for anon users (http://drupal.org/node/296453#comment-2397674) - is this the issue? Why then would it be working for anon users coming to my local server?

I can't think of another way to integrate this easily into webform as a hidden field. Perhaps I have to write some kind of custom hander to save the referrer?

  • Have you tried %server[HTTP_REFERER] as default value for your webform's hidden field? – Anoop Joseph Oct 17 '12 at 9:55
  • 1
    That won't work, because the value is recorded when the webform is submitted. And when it is submitted, the referring page is contact-us, so we would never see the real referring page. That's why I'm capturing the original referrer in a cookie. But thanks for the suggestion. – pglatz Oct 17 '12 at 16:09
1

So you can achieve this using form alter,

create a text field namely 'refer_url' for saving referring url, then using following snippet in your module you can save the referring url. You can hide this field in display using css or form alter.

function module_name_form_alter(&$form, &$form_state, $form_id) {
    if ($form_id == 'webform_client_form_27') { 
    $form['submitted']['refer_url']['#default_value'] = $_SERVER['HTTP_REFERER'];
    }
}
  • Same problem - when the page is first loaded, the default value is correctly set. I set a breakpoint in my form_alter. When the form is submitted, I reach it again, and this time the referrer is set to the webform, not the site I was coming from. That's why I tried using a cookie to save the value and stash it. – pglatz Oct 18 '12 at 17:06
  • Are you tried this with textfield, i got it working perfectly. However it is the same case as you said for the hiddenfield. Try with textfield and just hide it using css in frontend – Anoop Joseph Oct 19 '12 at 4:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.