Capture user IP, location and referring URL
Are you getting the most out of your subscriber signup forms?
I’m starting some new campaigns and wanted to get as much info as possible on my subscribers.
Here’s what I was after:
Record the referring URL
By recording the referring URL and monitoring user’s interaction with my newsletters, I can find out which pages or search phrases send the highest-converting traffic.
<?php $referer = $_SERVER['HTTP_REFERER']; echo $referer; ?>
I believe there are more advanced ways of doing this, including using cookies to record the original referrer (this code will only show the last page accessed, unfortunately), but this is plenty in my case.
Capture IPs
Freelancers will be pushing traffic to one of my signup forms on a CPA agreement. Monitoring IPs won’t be the greatest way to stop scammers, but it’s a quick and free solution without having to install any software.
This one’s fairly simple and will also help us capture the location in the next step.
<?php $IP = $_SERVER['REMOTE_ADDR']; echo $IP; ?>
Capture location
I’m promoting UK-only sites and this will help me segment the database to serve unique newsletter content accordingly.
There’s plenty of half-arsed tutorials on how to do this – including a solution where you upload an array of countries and their codes / IP ranges to a MySQL database and query them from there. This seemed like a fair bit of overkill to me and the vague instructions were beyond a coder at my level.
I was content with this solution found on the Tek-Tips forum.
// Create the country code key
<?php
$IP = $_SERVER['REMOTE_ADDR'];
$SSID = htmlentities(SID);
// If IP address exists
// Get country (and City) via api.hostip.info
if (!empty($IP)) {
$country=file_get_contents('http://api.hostip.info/get_html.php?ip='.$IP);
// Reformat the data returned (Keep only country and country abbr.
list ($_country) = explode ("\n", $country);
$_country = str_replace("Country: ", "", $_country);
} ?>
// Call your city and country info
<?php echo($country); ?> // Call City, Country
<?php echo($_country); ?>" // Call Country only
The ease of calling the API from hostip.info also means that you’re reliant on these guys keeping their database online, but I’m willing to bank on it for now.
Full signup form with IP, location, country
So here’s all our code in one place. Basically, this uses all of the above functions and inputs them into fields that users won’t see because of the display:none style.
To capture these in your subscriber database (I use and recommend Campaign Monitor), simply create a custom field where you can point the data.
<form id="sub" action="http://site.com/" method="post">
<input type="text" name="name-field" id="name" value="Name..." onfocus="value=''"/>
<br />
<input type="text" name="email-field" id="email" value="Email..." onfocus="value=''"/>
<input type="text" name="referrer-field" id="Referrer" value="<?php $referer = $_SERVER["HTTP_REFERER"]; echo $referer; ?>" style="display:none" />
<?php
$IP = $_SERVER['REMOTE_ADDR'];
$SSID = htmlentities(SID);
// If IP address exists
// Get country (and City) via api.hostip.info
if (!empty($IP)) {
$country=file_get_contents('http://api.hostip.info/get_html.php?ip='.$IP);
// Reformat the data returned (Keep only country and country abbr.
list ($_country) = explode ("\n", $country);
$_country = str_replace("Country: ", "", $_country);
}
?>
<input type="text" name="IP-field" id="IP" value="<?php echo($IP); ?>" style="display:none" /> // this was created in the country code above, so we can call it here also
<input type="text" name="country-field" id="Location" value="<?php echo($country); ?>" style="display:none" />
<input type="submit" value="Join" id="button" /></p>
</form>
Enjoy! Let me know if you have any questions of troubles.

putting this info into hidden fields is pointless and potentially buggy. Your form submits to a script, so get that info in the submission script away from th users form markup or scam robots interaction. The only exception would be th referrer info, which could stay in th hidden field, but again, its potentially buggy.
Dont forget the sanity checks serverside too.
How do you suggest I do that, working with the above code? Thanks for your advice.
The submission script would be the file the form gets posted to. I see in your “action” you have “http://site.com/”, lets start from there.
say your “action” is “http://site.com/submit.php”, the submission script would be submit.php
In submit.php, you would have all your validation:
check “name” is not empty with:
if(strlen($name) == ‘0′){
//do something on error
}
check “email” is valid with check_email_address() function found here: http://www.linuxjournal.com/article/9585
remember to do something on error again
After validation, you’d then do your IP check before inserting into the database and redirecting to the “Thank you” page.
The only problem would be that $_SERVER['HTTP_REFERRER'] in submit.php would be the page where your form is, so you’d still need that in your hidden fields. Or use cookies, which you’ve already ruled out.
The reason Im saying do the IP stuff in your submit page is:
A) hidden fields can be overwritten by robots
B) you’re depending on a 3rd party script for information, if there’s a hang at the 3rd party site, the form would take ages to load in and you’ll lose your user.
Your theory is sound enough Morris, it’s just the location of certain parts that could be improved to give a better user experience equaling more leads + more money for you.
Thanks a lot for your suggestions – I appreciate it.
I’m actually submitting the form to a third party (Campaign Monitor), so I’m not sure how possible it is to pass it through another submit.php type page and add the IP stuff there.
I also understand the downside of using a third party to find the location of the IP, though uploading and sourcing the country code MySQL stuff was a bit beyond me, this time
Thanks again.