I keep finding myself in situations where due to caching requirements it's more practical to use JavaScript for country specific functionality than doing it server side. I don't have a personal need for the city level lookup so I'm not spec-ing that out at this point but I've tried to choose names that don't preclude adding it later.
Module requirements:
- Add a menu callback
geoip/json/countrythat looks up the visitor's information and usesdrupal_json()to return the value. - When in debug mode we need to continue to allow the IP to be specified.
- We need to disable page caching for these requests:
$GLOBALS['conf']['cache'] = CACHE_DISABLED; - Add a helper function to handle calling
drupal_add_js()for our JS library.
JS requirements:
- Create a function named
Drupal.geoip.lookupCountryto make the AJAX requests to the server (Drupal.settings.basePath + 'geoip/json/country'). - The function should use a cookie to avoid repeated requests.
- The cookie lifetime should be overridable (perhaps by checking
Drupal.settings.geoip.lifetimefor a value?) but default to 24 hours. - The cookie path should be set to
Drupal.settings.basePath. - I'd prefer to avoid adding an external dependency on a cookie library, it should be simple enough to load and save the country code.
| Comment | File | Size | Author |
|---|---|---|---|
| #13 | geoip_1063566.patch | 2.97 KB | drewish |
| #12 | geoip_1063566.patch | 2.92 KB | drewish |
| #11 | geoip_1063566.patch | 4.15 KB | drewish |
| #10 | geoip_1063566.patch | 4.19 KB | drewish |
| #9 | geoip_1063566.patch | 3.49 KB | drewish |
Comments
Comment #1
bobooon commentedAttached is a patch and new JS file that adds country lookup support to GeoIP API.
Comment #2
drewish commentedI haven't opened up the JS since it's in a zip file but curious about the anti-caching code in hook_init(). For some reason I was thinking we could do that in the page callback. Does it have to be in hook_init?
Comment #3
bobooon commentedI'm actually not sure if it needs to be in hook_init() or in the page callback. It just seemed like hook_init() would be the safer choice, what do you think?
Comment #4
bobooon commentedAlso, should we use jquery.cookie to handle cookie read/writes?
Comment #5
bobooon commentedNew JS file corrects debug mode when using the JSON callback.
Comment #6
drewish commentedCool the text file is much easier to read. We should replace all the tabs with spaces in there and make sure we're using {} to wrap blocks of code in if else statements.
Looking at the lookupCountry() now I'm wondering if it makes more sense to have the caller pass in a callback function that we call from the AJAX call's success function. That way we don't have to block while we're waiting on the request. What do you think?
I kind of feel like the debug bit is a little weird. What do you think about trying to pull the geoip_debug from window.location.search ?
Comment #7
bobooon commentedAttached is a new patch file and javascript file. I updated the lookupCountry function to call a passed in function. I also added in the wondow.location.search regular expression for debug mode. Below is an example of how you would call lookupCountry and define a callback function.
Drupal.geoip.lookupCountry('Drupal.geoip_test');Comment #8
bobooon commentedSlightly updated javascript file that allows an extra parameter to be passed to the callback.
Comment #9
drewish commentedhere's a re-roll of another patch that rob had sent me.
Comment #10
drewish commentedmy re-roll dropped two functions, the json callback and the helper to add the js (which i've renamed geoip_add_js()). i also wrapped all the JS in a closure.
Comment #11
drewish commentedTightened up the regex a bit. Also removed the ability to pass the ip back as an argument. It seemed unused and made things a bit confusing.
Comment #12
drewish commentedNoticed a few issues with that last patch. Looks like if you're on a private IP e.g. 192.168.1.1 and the lookup fails the 'success' callback never fires. I feel like it should either fire or we should accept an error callback. For simplicity I just casted the result to a string so *something* comes back. Also fixed an undefined variable warning and cleaned up a couple of other small formatting issues.
Comment #13
drewish commentedFixed some bad comments.
Comment #14
drewish commentedCommitted to 6.x-1.x