/    Sign up×
Community /Pin to ProfileBookmark

I.P. to country

To customise a website. I was thinking of I.P. tracking them so the unit of price would change into their currency and all.

E.G.

When someone visits my site, their I.p. is recorded and the country of origin is also recorded. So then into a session cookie, AND into the My Sql database their I.P. along with their Country of origin is recorded. And a few things change in my website. Like Price, the money is automatically changed. The language and all.

Is this possible with PHP?

to post a comment
PHP

9 Comments(s)

Copy linkTweet thisAlerts:
@delr2691Jun 03.2007 — yes.. there are very big database tables distributed in internet containing IP ranges and the info related to it (country to which they belong, currency, language, etc)

lets say for example: from 64.0.0.1 to 64.0.0.189 belongs to usa, english, dollar, blah, blah..

so the first time the user logs in, the script finds the correct range to which the user ip belongs and loads the data like country, language, currency, etc.. then stores the user settings in the users table for not having to query the big ip table every time the user visits the site.. also, you could store it on a session variable so you dont even have to query the user table..
Copy linkTweet thisAlerts:
@felgallJun 03.2007 — One such database can be obtained from http://ip-to-country.webhosting.info/

noter that the address reflects the country where the ISP is located, not the individual user although in most cases these should be the same. Also if the user is accessing the web via a proxy server the country will be that of the pproxy server and not of the user.
Copy linkTweet thisAlerts:
@DragonkaiauthorJun 03.2007 — Well I guess you need a proxy tracking then..

Ok then, so say I have that database. So what do I load it on to? Text document? MYSQL? Access?

So what you mean is..

I write the code for getting the I.P. address

Than I match the first part of the I.P. to the ranges in the Database

A match is found, and I store that data in a cookie.

I know the code for getting the I.P.

and how to store data with cookie

And I think I know how PHP reads a database....

But I do not know how to match up the I.P. ranges together into the Countries

Could you give me snippet for that.

Thanks
Copy linkTweet thisAlerts:
@delr2691Jun 03.2007 — see if this works for you..

if not, post the error that outputs.. i havent test it..
[code=php]
//DO THIS IF THERE'S NO INFO IN THE USERS COOKIE,
//Otherwise, just read preferences from the cookie and store in session maybe

if ( !isset($_COOKIE['currency']) || empty($_COOKIE['currency']) ):

$userip = $_SERVER['REMOTE_ADDR'];

$sql='SELECT * FROM ip_table WHERE ipfrom <= '.$userip.' AND ipto >= '.$userip.' LIMIT 1'; //cols names depending on the table
$doSql = @mysql_query($sql, $conn); //where $conn is the mysql db connection
$data = @mysql_fetch_assoc($doSql);

$lang = $data['lang']; //depending on the table fields
$curr = $data['currency']; //the same

if (setcookie ("currency", $curr, time()+60*60*24*30*6) ) { //cookie expires in six months
echo 'cookie set with preferences'; //just for knowing that the cookie was set correcly
}

else:

//load $_COOKIE['currency'] for all the rest of the page, etc..

endif;
[/code]
Copy linkTweet thisAlerts:
@DragonkaiauthorJun 03.2007 — Ok...

I try that out tomorrow..

But one thing,

When I read the cookie, I want to change websites currency and what not...

Um... how do you do that?

Is it like Read cookie, and take one of the variables.. then if variable == into USA, Canada, China, etc.

then change the elements in the page, do I use Ajax to do such a thing, or should I just use.. um I don't know to change the whole entire page into another one?
Copy linkTweet thisAlerts:
@delr2691Jun 03.2007 — ok well.. the table in the link that felgall posted has a currency field so the script I posted will load the currency from the database directly (lets say, currency will be "US Dollar" if the IP of the user belongs to any country that has US Dollar as its currency).. that's understood..

The problem is at the moment of converting the prices from the original currency to the user's currency.. for that you could look for a site that provides xml data for currency convertion.. i dont know of any..

Also, you could use AJAX to change all the prices, but i recommend to do the whole convertion through PHP as the user wont be changing his currency that frequently!..

Let me know if i misunderstood your point, tomorrow.. I'll go to sleep right now, it's kinda late xD
Copy linkTweet thisAlerts:
@DragonkaiauthorJun 03.2007 — I know one..

http://www.xe.com/ucc/customize.php

Since thats an application.. Does that mean I have to use CGI to activate it?

Or is there some other code that executes programs.

Use PHP to change the currencies... that's an idea.
Copy linkTweet thisAlerts:
@delr2691Jun 03.2007 — no, you don't have to use CGI.. that application lets you add a calculator into your site.. it doesnt actually change the currency of your prices..

Maybe you should try this one that lets you show your prices into the currency the user wants.. Now, you don't need the database we were mentioning before to implement it, but if you want to automatically do the conversion (without the user having to select his currency), then you will have to first check the users currency in the database and tell the currency converter script to automatically show the prices in the new currency..
Copy linkTweet thisAlerts:
@DragonkaiauthorJun 03.2007 — OK then, thanx I'll play around with that.
×

Success!

Help @Dragonkai spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.20,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...