Drupal is known to have very good support for multi-site installations (with both shared content and / or just shared modules, themes and core codebase). Drupal is also known to have very good i18n (internalization) and l10n (localization). So in theory a website that needs to cater different content to different continents / regions plus translate it shouldn’t be a problem right? WRONG! 🙂
Here is one of the problems encountered and of course our solution in form of “magic” custom module…
The problem: the client wanted to use geo-location to redirect users to the right domain – content specific sub-site we were using the great domain module for managing the multisite content.
Surprised as we were, we couldn’t find a module for this basic use case, (through we saw other geoip modules switching the language but not one that would switch the domain) so here is the module.
Nothing fancy in the module’s info file:
1 2 3 4 |
name = GEOIP to continent description = GEOIP to continent package = Other core = 7.x |
In our module we also utilize the install file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 |
<?php /** * Implements hook_install(). */ function geoip2continent_install() { _geoip2continent_default_insert(); } /** * Implements hook_uninstall(). */ function geoip2continent_uninstall() { // todo: add any uninstall code } /** * Implements hook_schema(). */ function geoip2continent_schema() { $schema['geoip2continent'] = array( 'description' => 'Stores country to continent mapping', 'fields' => array( 'country' => array( 'type' => 'varchar', 'length' => 255, 'not null' => TRUE, 'default' => '', 'description' => 'country code', ), 'continent' => array( 'type' => 'varchar', 'length' => 255, 'not null' => TRUE, 'default' => '', 'description' => 'continent code', ), ), 'indexes' => array( 'name' => array('country'), 'surname' => array('continent'), ), ); return $schema; } /** * Inserts default entry into geoip2continent table */ function _geoip2continent_default_insert() { $sql = "INSERT INTO {geoip2continent} (country, continent) VALUES ('AD', 'EU'), ('AE', 'AS'), ('AF', 'AS'), ('AG', 'NA'), ('AI', 'NA'), ('AL', 'EU'), ('AM', 'AS'), ('AN', 'NA'), ('AO', 'AF'), ('AP', 'AS'), ('AQ', 'AN'), ('AR', 'SA'), ('AS', 'OC'), ('AT', 'EU'), ('AU', 'OC'), ('AW', 'NA'), ('AX', 'EU'), ('AZ', 'AS'), ('BA', 'EU'), ('BB', 'NA'), ('BD', 'AS'), ('BE', 'EU'), ('BF', 'AF'), ('BG', 'EU'), ('BH', 'AS'), ('BI', 'AF'), ('BJ', 'AF'), ('BL', 'NA'), ('BM', 'NA'), ('BN', 'AS'), ('BO', 'SA'), ('BR', 'SA'), ('BS', 'NA'), ('BT', 'AS'), ('BV', 'AN'), ('BW', 'AF'), ('BY', 'EU'), ('BZ', 'NA'), ('CA', 'NA'), ('CC', 'AS'), ('CD', 'AF'), ('CF', 'AF'), ('CG', 'AF'), ('CH', 'EU'), ('CI', 'AF'), ('CK', 'OC'), ('CL', 'SA'), ('CM', 'AF'), ('CN', 'AS'), ('CO', 'SA'), ('CR', 'NA'), ('CU', 'NA'), ('CV', 'AF'), ('CX', 'AS'), ('CY', 'AS'), ('CZ', 'EU'), ('DE', 'EU'), ('DJ', 'AF'), ('DK', 'EU'), ('DM', 'NA'), ('DO', 'NA'), ('DZ', 'AF'), ('EC', 'SA'), ('EE', 'EU'), ('EG', 'AF'), ('EH', 'AF'), ('ER', 'AF'), ('ES', 'EU'), ('ET', 'AF'), ('EU', 'EU'), ('FI', 'EU'), ('FJ', 'OC'), ('FK', 'SA'), ('FM', 'OC'), ('FO', 'EU'), ('FR', 'EU'), ('FX', 'EU'), ('GA', 'AF'), ('GB', 'EU'), ('GD', 'NA'), ('GE', 'AS'), ('GF', 'SA'), ('GG', 'EU'), ('GH', 'AF'), ('GI', 'EU'), ('GL', 'NA'), ('GM', 'AF'), ('GN', 'AF'), ('GP', 'NA'), ('GQ', 'AF'), ('GR', 'EU'), ('GS', 'AN'), ('GT', 'NA'), ('GU', 'OC'), ('GW', 'AF'), ('GY', 'SA'), ('HK', 'AS'), ('HM', 'AN'), ('HN', 'NA'), ('HR', 'EU'), ('HT', 'NA'), ('HU', 'EU'), ('ID', 'AS'), ('IE', 'EU'), ('IL', 'AS'), ('IM', 'EU'), ('IN', 'AS'), ('IO', 'AS'), ('IQ', 'AS'), ('IR', 'AS'), ('IS', 'EU'), ('IT', 'EU'), ('JE', 'EU'), ('JM', 'NA'), ('JO', 'AS'), ('JP', 'AS'), ('KE', 'AF'), ('KG', 'AS'), ('KH', 'AS'), ('KI', 'OC'), ('KM', 'AF'), ('KN', 'NA'), ('KP', 'AS'), ('KR', 'AS'), ('KW', 'AS'), ('KY', 'NA'), ('KZ', 'AS'), ('LA', 'AS'), ('LB', 'AS'), ('LC', 'NA'), ('LI', 'EU'), ('LK', 'AS'), ('LR', 'AF'), ('LS', 'AF'), ('LT', 'EU'), ('LU', 'EU'), ('LV', 'EU'), ('LY', 'AF'), ('MA', 'AF'), ('MC', 'EU'), ('MD', 'EU'), ('ME', 'EU'), ('MF', 'NA'), ('MG', 'AF'), ('MH', 'OC'), ('MK', 'EU'), ('ML', 'AF'), ('MM', 'AS'), ('MN', 'AS'), ('MO', 'AS'), ('MP', 'OC'), ('MQ', 'NA'), ('MR', 'AF'), ('MS', 'NA'), ('MT', 'EU'), ('MU', 'AF'), ('MV', 'AS'), ('MW', 'AF'), ('MX', 'NA'), ('MY', 'AS'), ('MZ', 'AF'), ('NA', 'AF'), ('NC', 'OC'), ('NE', 'AF'), ('NF', 'OC'), ('NG', 'AF'), ('NI', 'NA'), ('NL', 'EU'), ('NO', 'EU'), ('NP', 'AS'), ('NR', 'OC'), ('NU', 'OC'), ('NZ', 'OC'), ('OM', 'AS'), ('PA', 'NA'), ('PE', 'SA'), ('PF', 'OC'), ('PG', 'OC'), ('PH', 'AS'), ('PK', 'AS'), ('PL', 'EU'), ('PM', 'NA'), ('PN', 'OC'), ('PR', 'NA'), ('PS', 'AS'), ('PT', 'EU'), ('PW', 'OC'), ('PY', 'SA'), ('QA', 'AS'), ('RE', 'AF'), ('RO', 'EU'), ('RS', 'EU'), ('RU', 'EU'), ('RW', 'AF'), ('SA', 'AS'), ('SB', 'OC'), ('SC', 'AF'), ('SD', 'AF'), ('SE', 'EU'), ('SG', 'AS'), ('SH', 'AF'), ('SI', 'EU'), ('SJ', 'EU'), ('SK', 'EU'), ('SL', 'AF'), ('SM', 'EU'), ('SN', 'AF'), ('SO', 'AF'), ('SR', 'SA'), ('ST', 'AF'), ('SV', 'NA'), ('SY', 'AS'), ('SZ', 'AF'), ('TC', 'NA'), ('TD', 'AF'), ('TF', 'AN'), ('TG', 'AF'), ('TH', 'AS'), ('TJ', 'AS'), ('TK', 'OC'), ('TL', 'AS'), ('TM', 'AS'), ('TN', 'AF'), ('TO', 'OC'), ('TR', 'EU'), ('TT', 'NA'), ('TV', 'OC'), ('TW', 'AS'), ('TZ', 'AF'), ('UA', 'EU'), ('UG', 'AF'), ('UM', 'OC'), ('US', 'NA'), ('UY', 'SA'), ('UZ', 'AS'), ('VA', 'EU'), ('VC', 'NA'), ('VE', 'SA'), ('VG', 'NA'), ('VI', 'NA'), ('VN', 'AS'), ('VU', 'OC'), ('WF', 'OC'), ('WS', 'OC'), ('YE', 'AS'), ('YT', 'AF'), ('ZA', 'AF'), ('ZM', 'AF'), ('ZW', 'AF')"; db_query($sql); } |
now to the main module file, in our case we are using a free geoip api from http://freegeoip.net/ but you could replace it with any other geoip api.
we are also not going strict to continents so we added our own routing, here are the subsites we use:
- North America
- Europe
- Oceania / Australia
- South America / Brazil
- Hong Kong
- China / Rest of Asia
so here is the module code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
<?php function geoip2continent_init(){ // get current domain $domain = domain_get_domain(); $current_domain_id = $domain['domain_id']; // check if is front page and if current domain is the main domain if ((drupal_is_front_page() == TRUE) && ($current_domain_id == 1)){ $continent = getContinentCodeFromIP(); switch($continent){ case "NA": // user from North America $forward_to_domain = "2"; break; case "EU": // user from Europe $forward_to_domain = "3"; break; case "OC": // user from Oceania / Australia $forward_to_domain = "4"; break; case "SA": // user from South America / Brazil $forward_to_domain = "5"; break; case "HK": // user from Hong Kong $forward_to_domain = "6"; break; case "AS": // user from China / Asia $forward_to_domain = "7"; break; } // redirect domain_goto(domain_lookup($forward_to_domain)); } else { // if not front page don't redirect } } function getContinentCodeFromIP(){ $results = file_get_contents("http://freegeoip.net/csv/".$_SERVER['REMOTE_ADDR']); $results = explode(",",$results); $country = $results[1]; $country_code = str_replace('"','',$country); if($country_code == "HK"){ return $country_code; } $result = db_query('SELECT g.country, g.continent FROM {geoip2continent} g WHERE g.country = :country', array(':country' => $country_code))->fetchAll(); return $result[0]->continent; } |
As you see were are also redirecting only in case, that person comes to the home page.
Have a better way of accomplishing it? any comments on the above code? let me know in the comments below…