1

I am using this code to store the referrer URL in my form for tracking conversions, the problem I have is that the URL it stores is the URL which the form is on and not the URL where the visitor came from.

I am trying to get the URL of the website that the user came from, i.e if the user came from google I need the URL to be google.

Here's my code;

function hiddenreferer_shortcode($tag) {

    if ( ! is_array( $tag ) )
        return '';

    $options = (array) $tag['options'];
    foreach ( $options as $option ) {
        if ( preg_match( '%^name:([-0-9a-zA-Z_]+)$%', $option, $matches ) ) {
            $name_att = $matches[1];
        }
    }

     $pageURL = 'http';
     if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
     $pageURL .= "://";
     if ($_SERVER["SERVER_PORT"] != "80") {
        $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
     } else {
        $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
     }

     $value_att = $pageURL;
     $html = '<input type="hidden" name="' . $name_att . '" value="'.$value_att.'" />';
     return $html;
}
// REFERER IN CONTACT FORM 7
wpcf7_add_shortcode('hiddenreferer', 'hiddenreferer_shortcode', true);
Brad Fletcher
  • 3,547
  • 3
  • 27
  • 42
  • 3
    you need to use `$_SERVER['HTTP_REFERER']`, though it's not reliable - see http://stackoverflow.com/questions/6880659/in-what-cases-will-http-referer-be-empty and http://stackoverflow.com/questions/6023941/how-reliable-is-http-referer – billyonecan Mar 02 '16 at 13:52
  • 1
    To be honest, referral URL values (`HTTP_REFERER`) are easily adjusted by the browser or by the human browser or even in some browsers not even given at all, it's a significantly unsure way of getting that data, what reason do you want the data for? Can you use something like Google Analytics as a (pretty good) substitute? – Martin Mar 02 '16 at 13:53
  • 1
    before submitting your form, add a hidden input with $_SERVER['HTTP_REFERER'] but bear in mind - it can't be trusted, user can change it. – Muhammed Mar 02 '16 at 13:53
  • Possible duplicate of [Get original URL referer with PHP?](http://stackoverflow.com/questions/1864583/get-original-url-referer-with-php) – Skatox Mar 02 '16 at 14:01

1 Answers1

3

This may be a duplicate of: Get original URL referer with PHP?

in which case, you're looking for:

$_SERVER["HTTP_REFERER"];

Edit: As Martin reminded me in the comments below, as with all data coming from outside your controlled environment - sanitise it!

Escape the character sequence if you handle it in PHP and probably a good idea to check it for SQL injection attempts if you store it in a database at all (and use PDO/prepared statements etc).

Community
  • 1
  • 1
Minothor
  • 316
  • 4
  • 15
  • 3
    Perhaps a good idea to read the answer links you post in your answer and state: **And don't forget to escape $_SERVER["HTTP_REFERER"] since its a common attack vector for web apps.** – Martin Mar 02 '16 at 13:56
  • Agreed, and cheers for reminding me. I forget that the mantra of "sanitise, sanitise, sanitise" isn't always memorised. (lets face it, my job would be a lot easier if it was!) – Minothor Mar 02 '16 at 14:04