How to Whois in PHP?
I want to get the location of people logging into my website and I think it is called Whois. So how 开发者_运维问答would I go about locating people in PHP?
Get their IP using:
$_SERVER['REMOTE_ADDR']
and get a free database to convert ip to location :)
Here is one resource.
Also, you can find a host of free scrips out there.
You'll want to consider users behind proxies, as $_SERVER['REMOTE_ADDR']
may return the wrong IP address in that context. Check $_SERVER['HTTP_X_FORWARDED_FOR']
first to guard against this.
function getIpAddress() {
if (isset($_SERVER['HTTP_X_FORWARDED_FOR']) ) {
return $_SERVER['HTTP_X_FORWARDED_FOR'];
}
return $_SERVER['REMOTE_ADDR'];
}
you dont need to do whois. you need to grab their IP address and map it to a location. here's how you get the IP address
$_SERVER['REMOTE_ADDR']
Google provides free IP Address to location services. whenever you use google jsapi script google automatically populates the google.loader.ClientLocation which has all the juicy details you need.
A couple of examples
http://www.phpwhois.com/
http://www.nott.org/blog/php-whois-script.html
Sometimes shell_exec is the most elegant solution. Use at your own risk of course.
<?php
$domain = "stackoverflow.com";
echo shell_exec("host -t a $domain");
echo shell_exec("host -t mx $domain");
echo shell_exec("host -t ns $domain");
echo shell_exec("host -t txt $domain");
echo shell_exec("host -t cname $domain");
echo shell_exec("host -t soa $domain");
精彩评论