View Full Version : Link Tracking

07-14-2008, 09:49 PM
I'm sure that it's possible, but I'm wondering if it would be within the scope of a novice developer to do the following:

I would like to know on a form when the user has been referred to that form from another website. Particularly one parent website. That way we could differentiate between leads that were developed through our own ppc and leads developed from the parent site.

From the parent site there will be links directly to the form itself and to the index page (at which point they may later navigate to the form page).

We do not have access to the parent sites FTP so anything done would have to be from our side (we could give them a special link though).

Thanks for your help,

07-14-2008, 10:01 PM
Study PHP. Below are php functions that will read the IP address and the website your visitor came from, very usefull.


You then can use php to write this information to a database or txt file, I use just a simple txt file.



you could save said links to a db and then runs your stats off that

07-14-2008, 11:14 PM
REMOTE_ADDR will not give you the address you were referred from, its the attempt to resolve the IP of the browsing user.
HTTP_REFERER is not very reliable either as it needs to be sent from the originating server.
$HTTP_*_VARS are deprecated as well, you shouldn't rely on those.

You are better of sending a querystring through you're url for anything thats remote indicating what is what. Next best bet is to use the HTTP_REFERER, but make sure you account for if the remote browser doesn't send it.

07-15-2008, 04:38 AM
well personally i don't use that, but he's saying he doesn't have access to all the referring pages

07-15-2008, 05:08 PM
Wouldn't it also be possible to add a link to a special page that simple runs a short php script to assign variable $referred="yes" and then add something in the header to pass it along using GET to each page.

Then on the form page, no matter how many other pages the navigate through prior to filling out the form I can do a simple conditional to check referred?

Will this be feasible?

07-15-2008, 09:24 PM
Yes, this was my recommendation, but I'm not sure how you're considering the implementation. The idea would be to have say an affiliate site link to you with a special id indicating that its their site referring traffic to yours. If you do not have access to these other site or have a contact who can change it, you're next best bet is $_SERVER['HTTP_REFERER'] (yes its spelled wrong, I don't know why they did that >.<). Generally, I find that HTTP_REFERER is sent by other servers, but it is no guarantee (my personal server does not send referrer for example).

The idea that this would work is say I'm visiting a sister site, and I click on a link thats directed to you're site. You have a link thats like: http://youresite.com/?referrer=44646f5b77b1da67y322 for example and when I hit you're page you can establish the referrer from that point on. If you hash you're id's that are used to identify these sites you are more likely to keep the integrity of the referrals - if you do make sure the hashes are well thought out: md5(1) is pretty predictable, most people wouldn't waste their time on determining a 32byte length hash identifying a single referring id.

This is a technique that you would use to establish a relationship with a specific site that has been identified. This will also prevent someone from directing their browser manually from the other site to you're site out of coincidence - something I do not consider to be a referral. HTTP_REFERER is really more to get a basic idea of where people are coming from, search engines for example. Since there is no direct relationship between the two sites there is no priority to establish a solid referrer.

Does this make sense, I'm not great at explaining things, so let me know if I'm confusing you on anything!

07-17-2008, 05:15 PM
Once $referrer comes in from that website I would then have to pass it between each page in case they navigate around the site and then come back, correct?

Why would I need to hash this arbitrary string used to identify the referral website? I don't see any security issues with it, unless this is to prevent someone from changing the link, but it's either referred by this one person or not, it's not like there are a bunch of referralers (referries?).

Thanks so far! Going to work on implementing it today.

07-17-2008, 07:16 PM
You can get around passing the referrer around by placing it in a session variable. This allows you to 'hide' the referrer from the user browsing but keep track of it at the same time.
The hashing is not for security so much as for integrity. This depends of course on what its for and may not be necessary. Since the originals are passed through a url than technically a hash wouldn't help you at all - its just an idea to stop people from selecting arbitrary referrers while at you're site if you have a predictable format for you're referrers. This wouldn't be a big deal if you were say, tracking incoming users from you're sister site, but may be a bigger deal if you pay royalties for it. Do you understand what I'm getting at, I'm not great at explaining things, lol.

07-17-2008, 07:22 PM
I have it set up to work correctly now on the individual session page, but now I need to check into using sessions. I've never used them before and would like to learn.

Could you point me to a tutorial on the hashing? I have a brief understanding of hashing through cryptography, but have no idea how to do the implementation.

I think you're must better at explaining than you give yourself credit for. After all, you've been thanked 98 times in 95 posts :)

07-17-2008, 07:43 PM
PHP's builtin sessions are a piece of cake to work with, I found this one just now http://www.tizag.com/phpT/phpsessions.php. Seems pretty good, its brief, but covers it pretty good, and is using new session unlike most old ones (session_registers have been deprecated since 4.2).

session_start(); // Session start needs to be set before headers are sent Just put it in the first line always.
if (isset($_GET['referrer']))
$_SESSION['referrer'] = $_GET['referrer'];

You can retrieve a session anywhere you want, as long as that page has session_start() applied to the top. Access just like any other superglobal, but use the isset to check for existance.

Cryptography is complex as you know. Don't stress yourself and just use a builtin function like md5() or sha1() for example. Of course, the downside is you now need to do comparisons between the hashed value and the original value that has been hashed, but thats really no big deal either.

$hashedStr = md5($myStr);

Done and done!

Sessions have a default configuration that is kind of a pitfall. The directives are:

session.use_cookies = 1
session.use_only_cookies = 1
session.use_trans_sid = 0

This forces you to use cookies to accept sessions which can be a pain. If you're not in secure and open sessions are really not a huge threat, I'd change these directives:

ini_set('session.use_cookies', 1);
ini_set('session.use_only_cookies', 0);
ini_set('session.use_trans_sid', 1);


Its a pain, but the benefit is now you can use sessions without cookies enabled, and use_trans_sid will automatically append a session identifier to you're links and forms.

Hope that helps!

07-17-2008, 08:46 PM
Having some trouble with it for some reason.


// Referred? //

if (isset($_GET['ID'])) {
$_SESSION['referred'] = $_GET['ID'];

if ($_SESSION['referred'] == "1337")
$referred2 = "Referred from xyz.com";

$referred2 = " ";

$referred2 = " ";

// End Referral Code //


//Later in the form I have the email body formatted with details and $referred printed like //the rest of the variables:

$body .= "On $date, $fname $lname provided the following information:

$fname $lname
phone: $phone

Relocation Kit: $relocationdrop
Daily Emails: $emaildrop


Not sure why it's not picking up the $referred2 variable in the email body. I'll do some more troubleshooting in the meantime.

Update: When testing the variable referred2 is working properly and reports the correct value based upon the ID value, but for some reason the '$referred2' inside of the body formatting is not working. I don't think it's a scope issue, because I tested $referred2 right in front of the $body = and it worked fine.

07-17-2008, 08:58 PM
Change the way you are looking at it and always fetch from the session variables.

// This will prevent overwriting of the variable if it already exists.
if (!isset($_SESSION['referred']))
$_SESSION['referred'] = '';
if (isset($_GET['ID']))
$_SESSION['referred'] = $_GET['ID'];

// Then on whatever script you have (if its on a different one, make sure it has session_start on it too!)
$referred = '';
if (isset($_SESSION['referred']) && $_SESSION['referred'] == '1337')
$referred = "Referred from xyz.com";

Just use $referred instead of $referred2 on the message body.

You're coming along quite well, just keep tweaking it and eventually you'll get it to work :D

08-05-2008, 04:25 PM
Do I need to have the session started on all pages they can possibly navigate to? It doesn't keep the variable if they navigate away then come back to the form.