Forum Moderators: open

Message Too Old, No Replies

Using ASP to serve ads based on user location

some newbie help needed

         

Macro

12:13 pm on Nov 12, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I've bought a Frontpage website on shared hosting using NT and IIS. It's a fairly heavy traffic .co.uk site selling widgets but I want to cease selling widgets and change the content of the website to channel visitors to other merchants. However, there is no single merchant that covers both the US and UK markets. So I want to serve the user an ad based on where he is located or, even simpler, one ad for UK based traffic and a different ad for the US/the rest of the world.

I'm no programmer and php/asp fazes me. I can post asp code in a htm page (but don't want to rename the pages to asp or put redirect tags in each htm page header to point to an asp equivalent). I can also create include pages (SSIs) or iframes (I am very familiar with FP itself).

I raised this question in website technologies forum [webmasterworld.com] and encyclo kindly posted some PHP code that does this job and the suggestion was that someone here could give me the asp equivalent (no PHP support at this hosting service despite a price of £800 per year... but for various reasons can't move the site elsewhere just yet)

<?php
if (strpos($_SERVER["HTTP_ACCEPT_LANGUAGE"], 'en-gb')!== FALSE) {
include 'gb-aff-code.inc'; // include for GB customers
}
else {
include 'us-aff-code.inc'; // include for the others
}?>

Anyone with any ideas or suggestions? I'd like to get into learning PHP or asp but just don't have the time. As a small business owner the webmaster hat is only one of very many hats I wear.

bateman_ap

1:48 pm on Nov 12, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Do you have a databse you could use? If so I can post some code you can use with a freely avaialble IP Geo database to do this

Macro

2:14 pm on Nov 12, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



There is a database on the site and some asp pages connect to it so I could figure out the code currently used to connect to the database and modify yours accordingly. I'll give it a shot.

bateman_ap

3:04 pm on Nov 12, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Firstly you need to get the IP to country database. Check here to download.

[ip-to-country.webhosting.info...]

Download this, uncompress it and create a new table in your database. Call it something like ip_country.

Then put all the data from the file into this table. You should have 5 fields, IP_FROM, IP_TO, COUNTRY_CODE2, COUNTRY_CODE3, COUNTRY_NAME

OK, you can write some ASP now. Paste the following into a new file and call it ipaddressconvert.asp


<%
DottedIP = Request.ServerVariables("HTTP_X_FORWARDED_FOR")
if DottedIP = "" then
DottedIP = Request.ServerVariables("REMOTE_ADDR")
end if
If DottedIP = "" Then
Dot2LongIP = 0
Else
For i = 1 To 4
pos = InStr(PrevPos + 1, DottedIP, ".", 1)
If i = 4 Then
pos = Len(DottedIP) + 1
End If
num = Int(Mid(DottedIP, PrevPos + 1, pos - PrevPos - 1))
PrevPos = pos
Dot2LongIP = ((num Mod 256) * (256 ^ (4 - i))) + Dot2LongIP
Next
End If
%>

This basically gets a users ip address, then converts it into a long number that the database uses to look up the location.

Right, on the page you want to do a country lookup put in the following code:


<!--#include virtual="/ipaddressconvert.asp" -->
<%
Set conn = Server.CreateObject("ADODB.Connection")
conn.Open "PROVIDER=MSDASQL; DRIVER={SQL Server}; SERVER=localhost;DATABASE=location;UID=username;PWD=password;"
slocSQL = "SELECT COUNTRY_CODE3 FROM ip_country WHERE " & Dot2LongIP & " BETWEEN IP_FROM AND IP_TO"
Set RsLoc = Conn.Execute(slocSQL)
check_country = Trim((RsLoc.Fields("COUNTRY_CODE3").value))
IF RsLoc.EOF THEN
%>
<!--#include virtual="/your-other-file.asp" -->
<%
ELSE
IF check_country = "GBR" THEN
%>
<!--#include virtual="/your-uk-file.asp" -->
<%
ELSE
%>
<!--#include virtual="/your-other-file.asp" -->
<%
END IF
END IF
%>

What this does is connects to the database, looks up the users IP address and returns a country code for it. If the code = GBR (great britian) includes your uk file, otherwise it returns your generic file. You can use this to customise for any country in the database.

Macro

3:18 pm on Nov 12, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Great, I'll try this out. Thanks :)

>> Right, on the page you want to do a country lookup

Those are htm pages so I guess I can't put the asp code in there... I'll have to create a new mini asp page and use an i-frame to include it in the htm pages, right?

bateman_ap

3:35 pm on Nov 12, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If you have access to the server you can change it so html pages are run just like asp pages

Macro

4:01 pm on Nov 12, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



hmm, that's interesting. I didn't know that. Maybe if I put some pressure on them...they'll make the change.