Topic: Autodetect Country
Hi,
I want to be able to tell which country my user is in and change a bit of text on a certain page accordingly.
I was surprissed not to find a really simple html or javascript code for this, but the simplest solution I have found is accessing some server info via PHP code. I have the following code, but I'm wondering, can I just stick this inside the PHP file for the page of the info I want to change? Where would I put it? (I am a complete PHP novice, although I'm an ex JSP coder.
Advice is welcome.
$domains=array(
002 'ad'=>'Andorra',
003 'ae'=>'United Arab Emirates',
004 'af'=>'Afghanistan',
005 'ag'=>'Antigua and Barbuda',
006 'ai'=>'Anguilla',
007 'al'=>'Albania',
008 'am'=>'Armenia',
009 'an'=>'Netherlands Antilles',
010 'ao'=>'Angola',
011 'aq'=>'Antarctica',
012 'ar'=>'Argentina',
013 'as'=>'American Samoa',
014 'at'=>'Austria',
015 'au'=>'Australia',
016 'aw'=>'Aruba',
017 'az'=>'Azerbaidjan',
018 'ba'=>'Bosnia-Herzegovina',
019 'bb'=>'Barbados',
020 'bd'=>'Bangladesh',
021 'be'=>'Belgium',
022 'bf'=>'Burkina Faso',
023 'bg'=>'Bulgaria',
024 'bh'=>'Bahrain',
025 'bi'=>'Burundi',
026 'bj'=>'Benin',
027 'bm'=>'Bermuda',
028 'bn'=>'Brunei Darussalam',
029 'bo'=>'Bolivia',
030 'br'=>'Brazil',
031 'bs'=>'Bahamas',
032 'bt'=>'Bhutan',
033 'bv'=>'Bouvet Island',
034 'bw'=>'Botswana',
035 'by'=>'Belarus',
036 'bz'=>'Belize',
037 'ca'=>'Canada',
038 'cc'=>'Cocos (Keeling) Islands',
039 'cf'=>'Central African Republic',
040 'cg'=>'Congo',
041 'ch'=>'Switzerland',
042 'ci'=>'Ivory Coast (Cote D`Ivoire)',
043 'ck'=>'Cook Islands',
044 'cl'=>'Chile',
045 'cm'=>'Cameroon',
046 'cn'=>'China',
047 'co'=>'Colombia',
048 'cr'=>'Costa Rica',
049 'cs'=>'Former Czechoslovakia',
050 'cu'=>'Cuba',
051 'cv'=>'Cape Verde',
052 'cx'=>'Christmas Island',
053 'cy'=>'Cyprus',
054 'cz'=>'Czech Republic',
055 'de'=>'Germany',
056 'dj'=>'Djibouti',
057 'dk'=>'Denmark',
058 'dm'=>'Dominica',
059 'do'=>'Dominican Republic',
060 'dz'=>'Algeria',
061 'ec'=>'Ecuador',
062 'ee'=>'Estonia',
063 'eg'=>'Egypt',
064 'eh'=>'Western Sahara',
065 'er'=>'Eritrea',
066 'es'=>'Spain',
067 'et'=>'Ethiopia',
068 'fi'=>'Finland',
069 'fj'=>'Fiji',
070 'fk'=>'Falkland Islands',
071 'fm'=>'Micronesia',
072 'fo'=>'Faroe Islands',
073 'fr'=>'France',
074 'fx'=>'France (European Territory)',
075 'ga'=>'Gabon',
076 'gb'=>'Great Britain',
077 'gd'=>'Grenada',
078 'ge'=>'Georgia',
079 'gf'=>'French Guyana',
080 'gh'=>'Ghana',
081 'gi'=>'Gibraltar',
082 'gl'=>'Greenland',
083 'gm'=>'Gambia',
084 'gn'=>'Guinea',
085 'gp'=>'Guadeloupe (French)',
086 'gq'=>'Equatorial Guinea',
087 'gr'=>'Greece',
088 'gs'=>'S. Georgia & S. Sandwich Isls',
089 'gt'=>'Guatemala',
090 'gu'=>'Guam (USA)',
091 'gw'=>'Guinea Bissau',
092 'gy'=>'Guyana',
093 'hk'=>'Hong Kong',
094 'hm'=>'Heard and McDonald Islands',
095 'hn'=>'Honduras',
096 'hr'=>'Croatia',
097 'ht'=>'Haiti',
098 'hu'=>'Hungary',
099 'id'=>'Indonesia',
100 'ie'=>'Ireland',
101 'il'=>'Israel',
102 'in'=>'India',
103 'int'=>'International',
104 'io'=>'British Indian Ocean Territory',
105 'iq'=>'Iraq',
106 'ir'=>'Iran',
107 'is'=>'Iceland',
108 'it'=>'Italy',
109 'jm'=>'Jamaica',
110 'jo'=>'Jordan',
111 'jp'=>'Japan',
112 'ke'=>'Kenya',
113 'kg'=>'Kyrgyzstan',
114 'kh'=>'Cambodia',
115 'ki'=>'Kiribati',
116 'km'=>'Comoros',
117 'kn'=>'Saint Kitts & Nevis Anguilla',
118 'kp'=>'North Korea',
119 'kr'=>'South Korea',
120 'kw'=>'Kuwait',
121 'ky'=>'Cayman Islands',
122 'kz'=>'Kazakhstan',
123 'la'=>'Laos',
124 'lb'=>'Lebanon',
125 'lc'=>'Saint Lucia',
126 'li'=>'Liechtenstein',
127 'lk'=>'Sri Lanka',
128 'lr'=>'Liberia',
129 'ls'=>'Lesotho',
130 'lt'=>'Lithuania',
131 'lu'=>'Luxembourg',
132 'lv'=>'Latvia',
133 'ly'=>'Libya',
134 'ma'=>'Morocco',
135 'mc'=>'Monaco',
136 'md'=>'Moldavia',
137 'mg'=>'Madagascar',
138 'mh'=>'Marshall Islands',
139 'mil'=>'USA Military',
140 'mk'=>'Macedonia',
141 'ml'=>'Mali',
142 'mm'=>'Myanmar',
143 'mn'=>'Mongolia',
144 'mo'=>'Macau',
145 'mp'=>'Northern Mariana Islands',
146 'mq'=>'Martinique (French)',
147 'mr'=>'Mauritania',
148 'ms'=>'Montserrat',
149 'mt'=>'Malta',
150 'mu'=>'Mauritius',
151 'mv'=>'Maldives',
152 'mw'=>'Malawi',
153 'mx'=>'Mexico',
154 'my'=>'Malaysia',
155 'mz'=>'Mozambique',
156 'na'=>'Namibia',
157 'nc'=>'New Caledonia (French)',
158 'ne'=>'Niger',
159 'net'=>'Network',
160 'nf'=>'Norfolk Island',
161 'ng'=>'Nigeria',
162 'ni'=>'Nicaragua',
163 'nl'=>'Netherlands',
164 'no'=>'Norway',
165 'np'=>'Nepal',
166 'nr'=>'Nauru',
167 'nt'=>'Neutral Zone',
168 'nu'=>'Niue',
169 'nz'=>'New Zealand',
170 'om'=>'Oman',
171 'pa'=>'Panama',
172 'pe'=>'Peru',
173 'pf'=>'Polynesia (French)',
174 'pg'=>'Papua New Guinea',
175 'ph'=>'Philippines',
176 'pk'=>'Pakistan',
177 'pl'=>'Poland',
178 'pm'=>'Saint Pierre and Miquelon',
179 'pn'=>'Pitcairn Island',
180 'pr'=>'Puerto Rico',
181 'pt'=>'Portugal',
182 'pw'=>'Palau',
183 'py'=>'Paraguay',
184 'qa'=>'Qatar',
185 're'=>'Reunion (French)',
186 'ro'=>'Romania',
187 'ru'=>'Russian Federation',
188 'rw'=>'Rwanda',
189 'sa'=>'Saudi Arabia',
190 'sb'=>'Solomon Islands',
191 'sc'=>'Seychelles',
192 'sd'=>'Sudan',
193 'se'=>'Sweden',
194 'sg'=>'Singapore',
195 'sh'=>'Saint Helena',
196 'si'=>'Slovenia',
197 'sj'=>'Svalbard and Jan Mayen Islands',
198 'sk'=>'Slovak Republic',
199 'sl'=>'Sierra Leone',
200 'sm'=>'San Marino',
201 'sn'=>'Senegal',
202 'so'=>'Somalia',
203 'sr'=>'Suriname',
204 'st'=>'Saint Tome (Sao Tome) and Principe',
205 'su'=>'Former USSR',
206 'sv'=>'El Salvador',
207 'sy'=>'Syria',
208 'sz'=>'Swaziland',
209 'tc'=>'Turks and Caicos Islands',
210 'td'=>'Chad',
211 'tf'=>'French Southern Territories',
212 'tg'=>'Togo',
213 'th'=>'Thailand',
214 'tj'=>'Tadjikistan',
215 'tk'=>'Tokelau',
216 'tm'=>'Turkmenistan',
217 'tn'=>'Tunisia',
218 'to'=>'Tonga',
219 'tp'=>'East Timor',
220 'tr'=>'Turkey',
221 'tt'=>'Trinidad and Tobago',
222 'tv'=>'Tuvalu',
223 'tw'=>'Taiwan',
224 'tz'=>'Tanzania',
225 'ua'=>'Ukraine',
226 'ug'=>'Uganda',
227 'uk'=>'United Kingdom',
228 'um'=>'USA Minor Outlying Islands',
229 'us'=>'United States',
230 'uy'=>'Uruguay',
231 'uz'=>'Uzbekistan',
232 'va'=>'Vatican City State',
233 'vc'=>'Saint Vincent & Grenadines',
234 've'=>'Venezuela',
235 'vg'=>'Virgin Islands (British)',
236 'vi'=>'Virgin Islands (USA)',
237 'vn'=>'Vietnam',
238 'vu'=>'Vanuatu',
239 'wf'=>'Wallis and Futuna Islands',
240 'ws'=>'Samoa',
241 'ye'=>'Yemen',
242 'yt'=>'Mayotte',
243 'yu'=>'Yugoslavia',
244 'za'=>'South Africa',
245 'zm'=>'Zambia',
246 'zr'=>'Zaire',
247 'zw'=>'Zimbabwe');
248
249 $host = gethostbyaddr($_SERVER['REMOTE_ADDR']);
250 $domain = substr($host, strrpos($host,'.')+1);
251 if (isset($domains[$domain]))
252 {
253 echo 'Country: '. $domains[$domain];
254 }
255 else
256 {
257 echo 'Not found';
258 }