• 0

Capturing where user has came from?


Question

One of my clients wants applications sent to different email addresses based on where the user has came from.

I know i can use the referrer php code but when the user clicks the apply button the referrer changes to the home page url. if that make sense?

So what i have done is add this to the homepage:

<?php

$referrer = $_SERVER['HTTP_REFERER'];

?>

I need to pass the $referrer var to the contact page, and then it also needs to go into the sendmail.php script.

So i would use something like

<?php

if $referrer == "yahoo adwords link, whatever that may be?" {

$address = "firstaddress";

} elseif $referrer == "google adwords link, whatever that may be?" {

$address = "secondaddress";

}

?>

Basically my client wants to know where leads are coming from, if they come from yahoo he wants it to go to his yahoo address. If it comes from google adwords he wants it to go to his gmail address.

Link to comment
Share on other sites

7 answers to this question

Recommended Posts

  • 0

You could use a session variable or cookie to store $referrer when it's originally set, then on your contact page or whatever, just check for that session var/cookie's existence and do what you want with it.

Link to comment
Share on other sites

  • 0

Thanks, this is what i come up with but having problems.

Home page:


&lt;?php
session_start();

if (isset($_SESSION['ref'])) { unset($_SESSION['ref']); }

if (!isset($_SESSION['ref']) &amp;&amp; !empty($_SERVER['HTTP_REFERER']))
{
    $ref = htmlspecialchars($_SERVER['HTTP_REFERER']);
    $ref = urlencode($ref);
    $_SESSION['ref'] = $ref;
}
?&gt;

then when the user fills out a form and clicks submit this code should pick the address:

&lt;?php

$address = "xx@xx.com"; // main address

if (!empty($ref)) {
    $host = parse_url($ref, PHP_URL_HOST);
    if (in_array($host, 'google.co.uk')) {
        $address = 'xxx@gmail.com'; //ref is from google so send to different address. all other leads go to address above
    }
}
?&gt;

however its returning an error:

Warning: in_array() [function.in-array]: Wrong datatype for second argument in /home/username/public_html/join.php on line 61

any ideas?

Link to comment
Share on other sites

  • 0

no one? tried lots to try and fix it but cant :(

this is what $ref returns:

http%3A%2F%2Fwww.google.co.uk%2Fsearch%3Fsourceid%3Dchrome%26amp%3Bie%3DUTF-8%26amp%3Bq%3Djoin%2Bavon

Link to comment
Share on other sites

  • 0

Wrong datatype for the second argument in the in_array() function on line 61. Assuming the segment of code you posted is the area in question;

if (in_array($host, 'google.co.uk')) {

Doesn't make sense. The manual states that the 2nd argument must be an array. 'google.co.uk' isn't an array, it's a string. You probably want to do;

if(in_array('google.co.uk', $host)){

or something similar.

Top tip of the day;

Read the error messages :laugh:

Link to comment
Share on other sites

  • 0

That will still be wrong, because

$host = parse_url($ref, PHP_URL_HOST);

returns a string,

mixed parse_url ( string $url [, int $component = -1 ] )

This function parses a URL and returns an associative array containing any of the various components of the URL that are present.

Specify one of PHP_URL_SCHEME, PHP_URL_HOST, PHP_URL_PORT, PHP_URL_USER, PHP_URL_PASS, PHP_URL_PATH, PHP_URL_QUERY or PHP_URL_FRAGMENT to retrieve just a specific URL component as a string.

$host = parse_url($ref); would return an array,

$host = parse_url($ref, PHP_URL_HOST); would return a string

Link to comment
Share on other sites

  • 0

I believe you just wanted to do something like

&lt;?php
    $host = parse_url($ref, PHP_URL_HOST);
    if (stripos($host, 'google.co.uk') !== false) {
?&gt;

in_array() is to find an exact value in an array, strpos and its variants look for substrings in a string.

Read the error messages and always make sure you're working with the right value types! ;)

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.