I would like to show a sidebar advert to users only in a specific location (San Francisco). Would I sniff their IP, then if it fits in the San Fran IP area create an If/Else to show. If the IP != San Fran IP location then don't show?
Or do I not use IP to track their location these days?
The only realistic option is to use the user's IP address. This will not always be correct (will be wrong e.g. for proxy or VPN users), but workable. For this you need a database that maps IPs to countries (geolocation database).
There are many geolocation services available, but mostly not for free. See this answer: https://stackoverflow.com/questions/283016/know-a-good-ip-address-geolocation-service for details.
The answer is the whois database. Use the whois command on Linux, for example:
$ host www.stackoverflow.com
www.stackoverflow.com is an alias for stackoverflow.com.
stackoverflow.com has address 69.59.196.211
stackoverflow.com mail is handled by 20 alt1.aspmx.l.google.com.
stackoverflow.com mail is handled by 30 alt2.aspmx.l.google.com.
stackoverflow.com mail is handled by 40 aspmx2.googlemail.com.
stackoverflow.com mail is handled by 50 aspmx3.googlemail.com.
stackoverflow.com mail is handled by 10 aspmx.l.google.com.
$ whois 69.59.196.211
OrgName: Peak Inc.
OrgID: PEAKIN-3
Address: 1600 SW Western Suite #180
City: Corvallis
StateProv: OR
PostalCode: 97333
Country: US
...
Use the first "County" or "country" field. You will get the country of the network block which contains the IP. Try it at http://whois.domaintools.com/
Yes, you would use their IP address. You would need to download one of the following IP location databases and match the visitor's IP address to it:
http://ipinfodb.com/ip_database.php
That will give you their location.
Related
I have a database with user information (including IP Address) and I was wondering if it's possible to show the users location on a world map by getting the IP from the database in php?
Any help appreciated :)
To view the IP address of a visitor to your site, insert this code in the area where you want it to appear.
echo $_SERVER["REMOTE_ADDR"];
You can then use the API to locate your GeoLite MaxMind visitor
http://dev.maxmind.com/geoip/legacy/geolite/
It is possible to associate an IP address with a physical location, but only loosely. IP addresses are not registered to an individual user's exact location. You will be finding the location of the administrative domain of an IP address...but still on a world map that should be good enough.
Basically it works like this. You get a tool called geoip that should come packaged with a tool called geoiplookup. You then go to http://dev.maxmind.com/geoip/legacy/geolite/ to download GeoLiteCity.dat. Then you run this command.
geoiplookup -f /path/to/GeoLiteCity.dat <target IP or hostname>
For example, just now I ran
geoiplookup -f ~/Downloads/GeoLiteCity.dat stackoverflow.com
and I got this output
GeoIP City Edition, Rev 1: US, CA, California, San Francisco, 94107, 37.769699, -122.393303, 807, 415
Using this from PHP should be easy using the built in exec function from you PHP script.
I want a way to get the IP details like country name. I am getting the IP address using this code :
$_SERVER['REMOTE_ADDR'];
But I don't know how to get info of that IP.
IP address ranges are assigned to top providers that usually represent countries. The ranges are subdivided and sold to sub regions. This covers 90% of the case, although some ISPs can cover multiple countries, and addresses get bought and sold all the time. A commercial database is actively maintained. You may use such database (one example would be the MaxMind GeoIP database) to perform a reverse IP lookup. i.e. (IP -> geo division). There's no deterministic/mathematical way to look at the IP and "figure out" its origin. Location is not forwarded in the IP request.
I am wondering if it's possible to make my website only available to a specific country.
So anyone who tries to visit the website outside the specific country, will not have access.
I was thinking of getting a php IP Geo Locator script and do a redirect if they IP address is from the country I decide.
How would you guys recommend it?
Thank you.
EDIT
This doesn't have to be 100% accurate. Just need the php code that will do what I need.
The country is Romania by the way. Thanks! :)
The answer depends on what you mean by "available to a specific country".
What if an in-country computer has an IP address not on your list? That computer won't have access, but it's located in country.
What if an out-of-country computer has an IP address on your list? That computer will have address, but it's not located in country.
What if a router with an in-country IP address forwards requests from out-of-country? What if an in-country router is part of the TOR network?
What if a citizen leaves the country and wants to get access to your in-country web site from another country? The citizen will have an IP address not your list and will be denied access.
Until you define "available to a specific country" your question cannot be answered.
Hello
I am looking to write an independent script to log into an e-mail account [IMAP], get the headers for each e-mail, locate & store the (sender's or server's) IP address from each one. I recognize that this has to be a bit comprehensive to cover the top 3 webmail providers (Yahoo, Google & Hotmail) as well as the other common header formats.
Ideally, I would like to get the senders' IP addresses, but would settle for the servers' IP addresses.
I need to do this in PHP in a regular LAMP setup.
Any ideas would help. Thank you.
The details of the servers the system transits through are shown in the top of the email header in reverse-chronological order - the most recent are at the top, the first servers are at the bottom.
A quick, off-the-cuff solution would be to use a RegExp to try and find the bottom "Received:..." line containing an IP address.
A very quick test suggests that:
$regExp = '/Received:.*((?:\d+\.){3}\d+)/';
will match the lines, and return IP addresses.
Then you'd just use something like preg_match_all() to return an array of matched lines, and use the last one of the lot.
That IP is IP of server who mailed it, not the sender IP. Do you want servers IP or senders IP?
After you connected, got the messages list and now is foreaching a message:
$header=imap_fetchheader($imap,$msgn);
$regex='/client\-ip\=(.+?)\;/s';
preg_match_all($regex,$header,$matches);
$clientip=echo($matches[1]);
It works like a breeze for me.
I know this post is old but the above selected answer does get an IP but it does not necessarily get the correct sending server IP address. Below is the regex I used to get the actual sending servers IP:
/^Received:\sfrom(.*)[\[\(]\s*(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})[\]\)]/im
First of all thanks to all ....
I read that i can use the free IP geolocation webservice to get Country name from an Ip address. But how i get the response to my site , Actually this is what i look , In my site there is a need of displaying the country name from Ip address. Can uou please tell in detail... How can i display the country Name from that site ..
geoip_country_code_by_name can give you the country for an arbitrary IP address or hostname (not just the one from the current visitor as apache_note("GEOIP_COUNTRY_NAME") does), it uses the same MaxMind database. There is also the geoip_country_name_by_name function if you want the full country name rather than the ISO code.
http://www.maxmind.com/app/php
You need to install the php module and then use the following code to get the country name:
$country_name = apache_note("GEOIP_COUNTRY_NAME");
Have checked this functionality in Oplim? It is free for up to 500k pageviews, and you can set your code and custom condition (country, or even areas such as all the EU).
Disclosure: I am among the service developers.