Finding local time zones in rails - ruby-on-rails

I'm wondering if there is an easy way to look up a user's local time zone in Rails using only an IP address. I don't want users to have to input their time zone themselves. Do I have to use JavaScript or is there a different way?

The maxmind GeoLite IP->city database seems to support timezones and there's a FAQ on their site referring to this. You could either do a two-step process of IP->Location then Location->timezone using the Maxmind GeoLite City database and then use one of the solutions provided in the FAQ.
Or for a simple 1-step javascript, using getTimezoneOffset() seems to be the crux of the solution.

There appear to be several vendors offering APIs and callable services to go from ip address to location, and clearly once you have that determining the timezone is only a further lookup.
Your alternative of using javascript to ask the browser "where am I, what's the time zone" and Ajaxing that down to your server also sounds plausible.
Of course a sufficiently determined user can probably spoof their way to appearing to be at a different ip address, but presumably that doesn't matter too much to you ... their choice.

you can use the ip address-to-time API to find time by IP address.
Look here http://worldtimeengine.com/ for more details.
Dan

Related

Display content based on location

How do I go about displaying content based on a users location ? For ex. If somebody accesses the site from the New York , I would like to display New York Hotels . However if somebody accesses the site from Chicago , I would like to display Chicago hotels.
You're looking for a Geoloction database which would give you access to the typical IP ranges for the countries/cities you need to flag.
This is not absolute or completely trustworthy information though. Country level geo-location is mostly effective but anything like city/state/zip code level information should be treated with great caution.
I've worked with major multinational media providers using expensive paid services and discovered that the information in these databases is a very long way from correct and that users individual circumstances often prevent geo-location from being effective.
e.g. Virgin and East Coast trains in the UK use T-Mobile Germany as their onboard internet provider so you appear to be in Germany to many sites and payment processors.
There are quite a few free geolocation databases, MaxMind springs to mind (though this is not a recommendation of their service).
You can find some thoughts on implementing geo-location here
You need some database/api with information about hotells in different locations, then you need to now where the visitor is.
You can use something like Travel/Hotel API's? to find hotells.
And for finding the location of your visitor you can use something like http://www.hostip.info/use.html
or you can use HTML5 geolocation api example http://html5demos.com/geo . The bad thing with the html5 geo api is that the user need to accept before you get their location.
Remember that there is no guaranty that the location is correct...
This is the exact reason I created wpgeocode. WPGeocode is a free plugin for wordpress that enables publishers to customize content based on reader location. Check out the plugin at the support site at http://www.wpgeocode.com
The plugin enables shortcodes that can be placed in your posts or pages. There are many conditional shortcodes such as [wpgc_is_country_code country_code="US"] for this exact purpose. Simple open the shortcode, specify the target country_code and provide the content to be displayed if the reader is visiting from that specific country.
Visit http://www.wpgeocode.com/shortcodes for a complete listing - here are a few:
[wpgc_is_city_and_state city=”Yardley” state_code=”PA”]
[wpgc_is_ip” ip=”xx.xx.xx.xx”]
[wpgc_is_ips” ip=”xx.xx.xx.xx,aa.bb.cc.dd”]
[wpgc_is_not_ip” ip=”xx.xx.xx.xx”]
[wpgc_is_not_ips” ip=”xx.xx.xx.xx,aa.bb.cc.dd”]
[wpgc_is_city” city=””]
[wpgc_is_cities” cities=”city one,city two,city three”]
[wpgc_is_not_city” city=””]
[wpgc_is_not_cities” cities=”city
one,city two,city three”]
[wpgc_is_nearby”] – Uses the value you
specify in the Nearby Range setting from the administrative panel
[wpgc_is_not_nearby”]
[wpgc_is_within” miles=”10″]
[wpgc_is_within
kilometers=”12″]
[wpgc_is_country_name” country_name=””]
[wpgc_is_country_names” country_name=”United States,Egypt,Albania”]
[wpgc_is_country_code” country_code=””]
[wpgc_is_country_codes”
country_codes=”US,GB,AZ”]
[wpgc_is_state_code” state_code=””]
[wpgc_is_state_codes” state_codes=”PA,NJ,TX”]
[wpgc_is_not_country_name” country_name=””]
[wpgc_is_not_country_names” country_names=”United
States,Egypt,Albania”]
[wpgc_is_not_country_code” country_code=””]
[wpgc_is_not_country_codes” country_codes=”US,GB,AZ”]
[wpgc_is_not_state_code” state_code=””]
[wpgc_is_not_state_codes”
state_codes=”PA,NJ,TX”]
dotCMS offers the ability to geolocate content OTB (disclaimer, I work for them). You can see a demonstration that displays news content based on the user's location onthe demo site:
It is pretty easy to setup and use. Any type of content can be geolocated and the content can be accessed through the RESTful API. Under the covers, the Geolocation queries are handled natively via Elasticsearch.
Example:
http://demo.dotcms.com/demos/content-geolocation
Docs:
http://dotcms.com/docs/latest/es-geolocation-queries

Get country and language information from IP [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
IP to Country?
I would like to know how I can detect the language and country of a visitor to my website. It is my understanding that it is best to get this information from the IP but I don't know how to do that. Is this the method that large e-commerce sites use? If there is a database of location based on IP will it remain valid for a while or does that sort of information change rapidly?
It looks like using the IP is not the ultimate nor the best way to detect at least the language and also the country of a visitor.
As for the language, a good and effective way would be to use the http headers to get the language of the user´s browser or OS and then to adapt the language of the site to the collected data.
As MCannon mentions, in any case, it is always good to let the user change the language (as an example, my OS and browser are not in English but still, for some sites, I´d rather read and thus have the English version of the site).
As for the location of the user, I ran myself into such a problem and I was adviced by lethalMango a very good website and API that I am advising to you, so check the site IP info, where you can find a wonderful database and access it using their API
Hope it helps!

Get country location of an IP with native PHP

Read on before you say this is a duplicate, it's not. (as far as I could see)
I want to get the county code in php from the client.
Yes I know you can do this using external sites or with the likes of "geoip_record_by_name" but I don't want to be dependent on an external site, and I can't install "pear" for php as im using shard Dreamhost hosting.
I thought I could just do something like this:
$output = shell_exec('whois '.$ip.' -H | grep country | awk \'{print $2}\'');
echo "<pre>$output</pre>";
But dreamhost seems to have an old version of whois (4.7.5), so I get this error on allot of IPs:
Unknown AS number or IP network. Please upgrade this program.
So unless someone knows how to get a binary of a newer version of whois onto dreamhost im stuck.
Or is there another way I could get the country code from the client who is loading the page?
Whois is just a client for the whois service, so technically you are still relying on an outside site. For the queries that fail, you could try falling back to another site for the query, such as hostip.info, who happen to have a decent API and seem friendly:
http://api.hostip.info/country.php?ip=4.2.2.2
returns
US
Good luck,
--jed
EDIT: #Mint Here is the link to the API on hostip.info: http://www.hostip.info/use.html
MaxMind provide a free PHP GeoIP country lookup class (there is also a free country+city lookup one).
The bit you want is what is mentioned under "Pure PHP module". This doesn't require you to install anything, or be dependent on them, nor does it need any special PHP modules installed. Just save the GeoIP data file somewhere, then use their provided class to interact with it.
Can you just install a copy of whois into your home directory and pass the full path into shell_exec? That way you're not bound to their upgrade schedule.
An alternative, somewhat extreme solution to your problem would be to:
Download the CSV format version of MaxMind's country database
Strip out the information you don't need from the CSV with a script and ...
... generate a standard PHP file which contains a data structure containing the IP address as the key and the country code as the value.
Include the resulting file in your usual project files and you now have a completely internal IP => country code lookup table.
The disadvantage is that, regularly, you would need to regenerate the PHP file from the latest version of the database. Also, it's a pretty nasty way of doing it in general and performance might not be the best :)
Consider ipcountryphp (my site, my code, my honour) as it provides a local internet-lifetime freely updated database. It's fast and fully self-contained, pluggable into anything PHP 5.3, SQLite3 and beyond. Very fast seeks and no performance penalties.
Enough with shameless self-promotion, let's get serious:
Relying on querying remote services in real-time to get visitor country can become a major bottleneck for your site's functionality depending on the response speed of the queried server. As a rule of thumb you should never query external services for real-time site functionality (like page loading). Using APIs in the background is great but when you need to query the country of each visitor before the page is rendered, you open yourself up to a world of pain. And do keep in mind you're not the only one abusing free services :)
So queries to 3rd-party services stay in the background while only local functionality that relies on no 3rd-party go into the layers there users interact with. Just my slightly performance paranoid take on this :)
PS: Above mentioned script I wrote has IPv6 support too.
Here is a site with a script i just used. The only problem is that you would probably every now and then need to regenerate IPs by yourself... which might be pain and tahts why everyone is telling you to use external API. But for me that wasnt solution as i was pulling like 50 IPs at once, which means i would probably get banned. So solution was to use my own script or to do saves to DB, but i was again pulling images from external sites. Anyway here is the site i found script on:
http://coding-talk.com/f29/country-flag-script-8882/
Here's a few:
http://api.hostip.info/get_html.php?ip=174.31.162.48&position=true
http://geoiplookup.net/geoapi.php?output=json&ipaddress=174.31.162.48
http://ip-api.com/json/174.31.162.48?callback=yourfunction
http://ipinfo.io/174.31.162.48
All return slightly different results.
here is also one of them. just change the IP to the variable:
http://api.codehelper.io/ips/?callback=codehelper_ip_callback&ip=143.3.87.193

What's the best method to capture URLs?

I'm trying to find the best method to gather URLs, I could create my own little crawler but it would take my servers decades to crawl all of the Internet and the bandwidth required would be huge. The other thought would be using Google's Search API or Yahoo's Search API, but that's not really a great solution as it requires a search to be performed before I get results.
Other thoughts include asking DNS servers and requesting a list of URLs but DNS servers can limit/throttle my requests or even ban me all together. My knowledge of asking DNS servers is quite limited at the moment, so I don't know if this is the best method or not.
I just want a massive list of URLs, but I want to build this list without running into brick walls in the future. Any thoughts?
I'm starting this project to learn Python but that really has nothing to do with the question.
$ wget http://s3.amazonaws.com/alexa-static/top-1m.csv.zip
You can register to get access to the entire .com and .net zone files at Verisign
I haven't read the fine print for terms of use, nor do I know how much (if anything) it costs. However, that would give you a huge list of active domains to use as URLs.
How big is massive? A good place to start is http://www.alexa.com/topsites. They offer a download of the top 1,000,000 sites (by their ranking mechanism). You could then expand this list by going to Google and scraping the results of the query link: url for each url in the list.
modern terms now are URI and URN, URL is the shrunk/outdated. i'd scan for sitemap files that contain many addresses in one file and study the classic text spiders, wanderes, brokers and bots and RFC 3305 (appendix b. p 50) defining URI regex

ASP.NET Tracking Code & Unique Visitors

I am trying to find a way to track and produce reports for my site (out of interest). Does anyone know of any articles/projects etc that you can
Track pages / unique visitors etc
Tracking 1) relative to timestamp etc
in asp.net mvc or just asp.net ?
P.S - I know google analytics etc is available but looking to create some basic stats for myself out of interest about how web analytics work ?
There are a couple of good ways to try and determine unique visitors, none of them are exact (which is why different analytics will report different numbers).
The first is to use a cookie. Create a cookie for the user for each time frame that you want to track uniques, so you could create one that expires in a day and one that expires in a month. You can then use both of those to track how many unique daily/monthly visitors you have. Of course this is not perfect since people can clear or refuse cookies, but it is pretty accurate.
The other way is to track uniques using a combination of the IP address and User Agent of the requesting user, this is probably slightly less accurate since if a company has a good IT group lots of internal users will have the same User Agent and since they are all coming from the same internal network could have the same IP address.
If you are interested in reading more about the different methods there is a great article about it here: http://www.google.com/support/urchin45/bin/answer.py?answer=28325
I blogged about simple asp.net module.
You can check it here
http://ilkeraksu.com/post/2009/07/14/Very-very-simple-But-very-very-efficient-Aspnet-Tracking-module.aspx
I would recommend using google analytics instead of reinventing the wheel. All you have to do is stick a bit of javascript in your master page and your done.
Yo can check Piwik out. Its an open source web analytics written using PHP and mysql.
you can find great article in http://www.codeproject.com/KB/aspnet/PageTracking.aspx
which is upgraded version of http://www.15seconds.com/Issue/021119.htm
with help of a Session Tracker class that runs in Application_PreRequestHandlerExecute and mailing reports on session end and lot of usefull tips
thanks Wayne Plourde for all that stuff

Resources