<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>iamseo.org &#187; User Location</title>
	<atom:link href="http://www.iamseo.org/tag/user-location/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.iamseo.org</link>
	<description>Just another (SEO) Wordpress weblog</description>
	<lastBuildDate>Mon, 31 May 2010 08:08:41 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Capture user IP, location and referring URL</title>
		<link>http://www.iamseo.org/how-to/capture-user-ip-location-and-referring-url/</link>
		<comments>http://www.iamseo.org/how-to/capture-user-ip-location-and-referring-url/#comments</comments>
		<pubDate>Sun, 31 Jan 2010 10:40:47 +0000</pubDate>
		<dc:creator>Morris</dc:creator>
				<category><![CDATA[How To]]></category>
		<category><![CDATA[Capture IP]]></category>
		<category><![CDATA[Capture Location]]></category>
		<category><![CDATA[Capture Referrer]]></category>
		<category><![CDATA[Capture Referring URL]]></category>
		<category><![CDATA[Newsletter Signup Form]]></category>
		<category><![CDATA[Subscriber Field]]></category>
		<category><![CDATA[Subscriber Form]]></category>
		<category><![CDATA[User Country]]></category>
		<category><![CDATA[User Location]]></category>

		<guid isPermaLink="false">http://www.iamseo.org/?p=105</guid>
		<description><![CDATA[Are you getting the most out of your subscriber signup forms?
I&#8217;m starting some new campaigns and wanted to get as much info as possible on my subscribers.
Here&#8217;s what I was after:
Record the referring URL
By recording the referring URL and monitoring user&#8217;s interaction with my newsletters, I can find out which pages or search phrases send [...]]]></description>
			<content:encoded><![CDATA[<p>Are you getting the most out of your subscriber signup forms?</p>
<p>I&#8217;m starting some new campaigns and wanted to get as much info as possible on my subscribers.</p>
<p>Here&#8217;s what I was after:</p>
<h2>Record the referring URL</h2>
<p>By recording the referring URL and monitoring user&#8217;s interaction with my newsletters, I can find out which pages or search phrases send the highest-converting traffic.</p>
<p><span id="more-105"></span></p>
<pre class="brush: php;">
&lt;?php $referer = $_SERVER['HTTP_REFERER']; echo $referer; ?&gt;
</pre>
<p>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. </p>
<h2>Capture IPs</h2>
<p>Freelancers will be pushing traffic to one of my signup forms on a CPA agreement. Monitoring IPs won&#8217;t be the greatest way to stop scammers, but it&#8217;s a quick and free solution without having to install any software.</p>
<p>This one&#8217;s fairly simple and will also help us capture the location in the next step. </p>
<pre class="brush: php;">
&lt;?php $IP = $_SERVER['REMOTE_ADDR']; echo $IP; ?&gt;
</pre>
<h2>Capture location</h2>
<p>I&#8217;m promoting UK-only sites and this will help me segment the database to serve unique newsletter content accordingly. </p>
<p>There&#8217;s plenty of half-arsed tutorials on how to do this &#8211; 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.</p>
<p>I was content with this solution found on the <a href="http://www.tek-tips.com/viewthread.cfm?qid=1467015&#038;page=1">Tek-Tips forum</a>. </p>
<pre class="brush: php;">
 // Create the country code key

&lt;?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 (&quot;\n&quot;, $country);
	$_country = str_replace(&quot;Country: &quot;, &quot;&quot;, $_country);
	} ?&gt;

// Call your city and country info

&lt;?php echo($country); ?&gt; // Call City, Country

&lt;?php echo($_country); ?&gt;&quot; // Call Country only
</pre>
<p>The ease of calling the API from hostip.info also means that you&#8217;re reliant on these guys keeping their database online, but I&#8217;m willing to bank on it for now. </p>
<h2>Full signup form with IP, location, country</h2>
<p>So here&#8217;s all our code in one place. Basically, this uses all of the above functions and inputs them into fields that users won&#8217;t see because of the display:none style. </p>
<p>To capture these in your subscriber database (I use and recommend Campaign Monitor), simply create a custom field where you can point the data. </p>
<pre class="brush: php;">
&lt;form id=&quot;sub&quot; action=&quot;http://site.com/&quot; method=&quot;post&quot;&gt;
	&lt;input type=&quot;text&quot; name=&quot;name-field&quot; id=&quot;name&quot; value=&quot;Name...&quot; onfocus=&quot;value=''&quot;/&gt;
	&lt;br /&gt;
	&lt;input type=&quot;text&quot; name=&quot;email-field&quot; id=&quot;email&quot; value=&quot;Email...&quot; onfocus=&quot;value=''&quot;/&gt;

	&lt;input type=&quot;text&quot; name=&quot;referrer-field&quot; id=&quot;Referrer&quot; value=&quot;&lt;?php $referer = $_SERVER[&quot;HTTP_REFERER&quot;]; echo $referer; ?&gt;&quot; style=&quot;display:none&quot; /&gt;

		&lt;?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 (&quot;\n&quot;, $country);
		$_country = str_replace(&quot;Country: &quot;, &quot;&quot;, $_country);
		}
		?&gt;

	&lt;input type=&quot;text&quot; name=&quot;IP-field&quot; id=&quot;IP&quot; value=&quot;&lt;?php echo($IP); ?&gt;&quot; style=&quot;display:none&quot; /&gt; // this was created in the country code above, so we can call it here also

	&lt;input type=&quot;text&quot; name=&quot;country-field&quot; id=&quot;Location&quot; value=&quot;&lt;?php echo($country); ?&gt;&quot; style=&quot;display:none&quot; /&gt;

	&lt;input type=&quot;submit&quot; value=&quot;Join&quot; id=&quot;button&quot; /&gt;&lt;/p&gt;
	&lt;/form&gt;
</pre>
<p>Enjoy! Let me know if you have any questions of troubles. </p>
]]></content:encoded>
			<wfw:commentRss>http://www.iamseo.org/how-to/capture-user-ip-location-and-referring-url/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>
