PDA

View Full Version : Auto-Detect User Location At Registration


Raz
06-05-2003, 10:00 PM
Simple hack making use of the Directi Ip-to-Country Database (http://www.directi.com/?site=ip-to-country) (included).

What it does
Auto-detects which country user is from and auto-fills the location field on the registration form using their IP Address.

Installing
Import the 'ip2country' table attached into your forums database. Either use phpMyAdmin or from ssh.
(Make sure your mysql max_packet size is large enough to handle the ~2 Mb file -- otherwise try downloading this split table here (https://vborg.vbsupport.ru/showthread.php?postid=405522#post405522))

Open up register.php and look for: while ($profilefield=$DB_site->fetch_array($profilefields)) {
$profilefieldname = "field$profilefield[profilefieldid]";
Above that place the following:// CUSTOM ADD AUTO LOCATION
$ip_number = sprintf("%u",ip2long($REMOTE_ADDR));
$ct_array = $DB_site->query_first("SELECT COUNTRY_NAME FROM ip2country WHERE $ip_number >= IP_FROM AND $ip_number <= IP_TO LIMIT 1");
$bbuserinfo['field2'] = ucwords(strtolower($ct_array['COUNTRY_NAME']));
// END CUSTOM ADD AUTO LOCATIONOnce you've done that, you're all set.

cirisme
06-06-2003, 01:26 PM
Cool! 8-)

Moving servers right now, but I will install this when I am done. :)

Tim Wheatley
06-06-2003, 02:11 PM
Doesn't work here... Nothing changes at all...

msimplay
06-06-2003, 02:13 PM
didn't work for me on vbulletin 2.3

partang2
06-06-2003, 04:25 PM
Maybe that is because phpMyAdmin doesn't support files as large as this...?

Tim Wheatley
06-06-2003, 05:34 PM
It does, the queries worked eventually. Took a while though. The problem is it's just not working, don't know the reason why though...

-Sidekick-
06-06-2003, 05:53 PM
That sucks. I'll have to keep checking back then. Cause I hate when people leave there Location empty and then it shows up blank in the postbit.

Raz
06-06-2003, 07:07 PM
Apologies. There are many modifications on my boards, which is why it isn't working on others.

I've installed a vanilla version, and modified the instructions to reflect normal installations. So please try those now :)

Also make sure that your mysql max_packet variable is large enough to handle a 2MB file. I would personally recommend importing the table via ssh.

Boofo
06-06-2003, 07:16 PM
Great idea but you really need to make the query into a few smaller ones for those of us that don't have SSH. ;)

Raz
06-06-2003, 07:30 PM
Have you tried importing it from phpMyAdmin? It may be large, but its not impossible.

Mike11212
06-06-2003, 07:36 PM
dont you have to pay for ip2country database?

Raz
06-06-2003, 07:50 PM
From the site (link posted in first post):The IP-to-Country Database and Tools are available FREE of charge.

Broekie
06-06-2003, 07:50 PM
Today at 10:36 PM Mike11212 said this in Post #11 (https://vborg.vbsupport.ru/showthread.php?postid=405284#post405284)
dont you have to pay for ip2country database?

Yes, you do. You should remit $100 to my swiss bank-account :p


Okay, I'm kidding...
Here's the serious part of this post:
The fourth line of text on http://www.ip-to-country.com:
The IP-to-Country Database and Tools are available FREE of charge.

Boofo
06-06-2003, 08:09 PM
Today at 03:30 PM Raz said this in Post #10 (https://vborg.vbsupport.ru/showthread.php?postid=405282#post405282)
Have you tried importing it from phpMyAdmin? It may be large, but its not impossible.

I tried to do a 2 meg file when I had the Weather hack installed and the author had to make smaller files so others could import them as well.

Tim Wheatley
06-06-2003, 08:09 PM
Yes it works now.

I got it to work when someone updates their profile too:

In member.php above while ($profilefield=$DB_site->fetch_array($profilefields)) {
$profilefieldname="field$profilefield[profilefieldid]";

Add the same as in first post.

Works great, thank you. I'm using 2.3.0.

Raz
06-06-2003, 08:11 PM
Today at 10:09 PM Boofo said this in Post #14 (https://vborg.vbsupport.ru/showthread.php?postid=405301#post405301)
I tried to do a 2 meg file when I had the Weather hack installed and the author had to make smaller files so others could import them as well. Feel free to do it yourself.

AlexanderT
06-07-2003, 08:28 AM
Really nice! *installed*

I attached a split version of the ip2country table. Make sure you first insert part1 before inserting the other parts (in order to create the table).

Alex

partang2
06-07-2003, 10:17 AM
Thanks a lot AlexanderT!! :)

Raz
06-07-2003, 10:37 AM
I've added a link to the split table in the first post.

Boofo
06-07-2003, 11:46 AM
Can someone tell me how many rows there are and what size the table should be? I want to make sure I got it in there right.

Raz
06-07-2003, 11:47 AM
Here are my stats:

Data 1,448 KB
Index 699,392 Bytes
Total 2,131 KB

Rows 56,576
Row length ø 26
Row size ø 39 Bytes

Boofo
06-07-2003, 12:03 PM
Bingo! We have a winner. Thank you, sir. ;)

* Boofo sneaks off to click install

zajako
06-09-2003, 02:54 AM
is there a free city listing set of tables, or does this one list the city,Country

i know its possible to determine city from ip but im sure that would make the db jump several megs. Im willing to do it if there is one avaliable.

of course ill use it a lot more than just showing in their location ;)

Boofo
06-09-2003, 03:48 AM
That would be a great idea if the IPs always returned to correct city. Mine shows Alexandria, Virginia, USA and I am in Des Moines, Iowa USA. Sometimes it shows the headquarters of your ISP instead.

S.Shady
06-09-2003, 03:55 AM
or the proxie your on :) as i found out today thought somone was in la turns out they were in washington :-\

Boofo
06-09-2003, 04:04 AM
That's true, but I am not on a proxy and it still shows my city and state wrong.

S.Shady
06-09-2003, 04:21 AM
i didnt mean you :) i ment a general user - but its weird its wrong.

Boofo
06-09-2003, 04:40 AM
No, No, I know you weren't talking about me. ;) I was just trying to let you know that proxies aren't the only ones that can be wrong. You're right. It is weird. ;)

zajako
06-09-2003, 04:55 AM
yeah but even if it is worng sometimes, it still captures a majority of the users.

S.Shady
06-09-2003, 05:09 AM
Today at 01:55 AM zajako said this in Post #29 (https://vborg.vbsupport.ru/showthread.php?postid=406414#post406414)
yeah but even if it is worng sometimes, it still captures a majority of the users.


true

Raz
06-09-2003, 08:39 AM
I don't know of a freely available database that does this.

For larger ISPs it would probably be wrong, as it would just show the HQ of the ISP (who the IPs are registered to).

Pikok
07-14-2003, 05:26 AM
I made a small change to the code Tim Wheatley posted for the "member.php" file as follows..

Add:// CUSTOM ADD AUTO LOCATION
$ip_number = sprintf("%u",ip2long($REMOTE_ADDR));
$ct_array = $DB_site->query_first("SELECT COUNTRY_NAME FROM ip2country WHERE $ip_number >= IP_FROM AND $ip_number <= IP_TO LIMIT 1");
if (!$bbuserinfo['field2']) {
$bbuserinfo['field2'] = ucwords(strtolower($ct_array['COUNTRY_NAME']));
}
// END CUSTOM ADD AUTO LOCATION
Above: while ($profilefield=$DB_site->fetch_array($profilefields)) {
$profilefieldname="field$profilefield[profilefieldid]";

With the code Tim had posted, the location would be automatically changed to just the country whenever a user modified their profile. With the change I made though, the location will only be automatically changed if the location field is empty (i.e. the user hasn't filled in a location manually).

In other words, if the user already has something in the location field it won't be changed. If the location field is empty, it will be automatically filled in with the country name.

Pikok
07-14-2003, 11:44 PM
I also made the following change so no user can leave the location field empty in their profile..

In "root/member.php" find: // check extra profile fields
$userfields="";
$profilefields=$DB_site->query("SELECT profilefieldid,required,title,size,maxlength
FROM profilefield
WHERE editable = 1");And Add Below It:// CUSTOM ADD AUTO LOCATION
if (!$field2) {
$ip_number = sprintf("%u",ip2long($REMOTE_ADDR));
$ct_array = $DB_site->query_first("SELECT COUNTRY_NAME FROM ip2country WHERE $ip_number >= IP_FROM AND $ip_number <= IP_TO LIMIT 1");
$field2 = ucwords(strtolower($ct_array['COUNTRY_NAME']));
}
// END CUSTOM ADD AUTO LOCATION
In "root/register.php" find: // check extra profile fields
$userfields="";
$userfieldsnames="(userid";
$profilefields=$DB_site->query("SELECT maxlength,profilefieldid,required,title
FROM profilefield
WHERE editable = 1
ORDER BY displayorder");And Add Below It:// CUSTOM ADD AUTO LOCATION
if (!$field2) {
$ip_number = sprintf("%u",ip2long($REMOTE_ADDR));
$ct_array = $DB_site->query_first("SELECT COUNTRY_NAME FROM ip2country WHERE $ip_number >= IP_FROM AND $ip_number <= IP_TO LIMIT 1");
$field2 = ucwords(strtolower($ct_array['COUNTRY_NAME']));
}
// END CUSTOM ADD AUTO LOCATION
With these additions, if a user leaves the location field empty (either when registering or updating their profile), the country will be automatically inserted into the database. Resulting in no empty locations. ;)

tinbin
07-17-2003, 12:33 PM
How could i combine this hack with the Flagshack?
So that it looks up what country the IP is in and then update the countryflag next to the username.

Would this be hard to do?

PurpleCow
08-10-2003, 05:38 AM
Pikok - The changes you mentioned, will they be the same for ver 2.3.0 also ??

I am really interested in this hack, let me know how your changes and this hack works on 2.3.0 ??

Thank you very much

Cheers

msibm
02-09-2007, 06:21 AM
Are there new version for 3.6.4?
Thanks.

steven s
02-10-2007, 12:21 AM
I was searching for something else entirely and came across this.

How could i combine this hack with the Flagshack?
So that it looks up what country the IP is in and then update the countryflag next to the username.

Are there new version for 3.6.4?
Thanks.

I was thinking about the same thing about both posts.