...

View Full Version : Capturing where user has came from?



forcerhpool
11-22-2010, 08:04 PM
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.

mlseim
11-22-2010, 08:28 PM
How about using Google Analytics?
That would go right along with Adsense.

forcerhpool
11-22-2010, 08:37 PM
How about using Google Analytics?
That would go right along with Adsense.

we already use that but i think he wants to go down the separate email route

Fou-Lu
11-22-2010, 08:48 PM
If the HTTP_REFERER (yes its spelled wrong in PHP) is given to you, you can determine its base url with the parse_url function. This in combination with an in_array will give you what you need.


$ref = $_SERVER['HTTP_REFERER'];
$addr = 'somedefaultaddress';
if (!empty($ref))
{
$host = parse_url($ref, PHP_URL_HOST);
if (in_array($host, 'yahoo.com'))
{
$addr = 'yahooaddress';
}
}

And so forth. parse_url returns an array by default that parses each little thing, but will not separate the querystring. This example only captures the host part (which I'm fairly certain includes the suffix). Use an in_array or a switch to use the correct address.

forcerhpool
11-22-2010, 09:01 PM
thanks thats great. How can i push the REFERER to the apply page.

google > site index page > apply page

Fou-Lu
11-22-2010, 09:08 PM
To persist? Use a session for that, which actually works very nicely:


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

Then when you need to use the referrer check, extract from $_SESSION['ref'] instead of $_SERVER['HTTP_REFERER']. May need to play around with the changes on the htmlspecialchars / urlencode there. Sadly, referrer is tainted data.
Oh, you'll need session_start called prior to any use of the $_SESSION, so add that to the top of the apply page before doing the checks.

forcerhpool
11-22-2010, 09:34 PM
I've made it work but the session is keeping old values even when i re access the site

for example clicking from google gives google.com ref then i'll go to yahoo and it will give google.com still. i can use session_destroy() which works but it then kills everything so cant push it to next page

any ideas?

MattF
11-22-2010, 11:54 PM
When they reach the assigned page:



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

forcerhpool
11-23-2010, 12:03 PM
Thanks MattF that worked.

When i click submit on the form its giving this error:

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

MattF
11-23-2010, 05:09 PM
Assuming that's the in_array line Fou-Lu posted?



$host = parse_url($ref, PHP_URL_HOST);
if (stripos($host, 'yahoo.com') !== false)

forcerhpool
11-23-2010, 06:43 PM
its working many thanks



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum