why geoip not works for me - php

I am trying to working with geoip to display Ips and their Countries . I am trying to display this code but nothing comes up?
$country = geoip_country_name_by_name($ip);

You must have the GeoIP functions installed first. If they are installed, then it could be possible, that the IP you're providing to the function does not exist in the database.
Try this code:
<?php
$country = geoip_country_name_by_name($ip);
if ($country) {
echo 'This host is located in: ' . $country;
} else {
echo 'Cannot find the IP in the database.'
}

Related

Does Google Geocode work for anyone in 2018 is there an alternative?

Google Geocode use to work for me but does not anymore. Everything I've tried returns false. It doesn't even give a clue as to why. I've tried changing the API key, I've tried different methods to connect (I prefer using simplexml_load_file). It does work if I type the URL directly into the a browser window but not from my website or even from my localhost. Does anyone have suggestions on what to try next or an alternative to Googles Geocode service?
Here is some code that I've been using that Json / XML results.
//URL SAVE ADDRESS
date_default_timezone_set("America/Chicago");
$address = "1600 Amphitheatre Parkway, Mountain View, CA 94043";
$address = urlencode($address);
// GOOGLE URL
$url = "https://maps.googleapis.com/maps/api/geocode/xml?address=". $address ."&sensor=false&key=AIzaSyDrGe_qr4ofea8WhZFhnPGsXNQtTiQwGhw";
echo $url; //TEMP
$xml = simplexml_load_file($url) OR $ex['tblx'] = "Unable to load XML from Googleapis.";
if(empty($xml)) {
$ex['tblx'] = "Googleapis return no values.";
} elseif("" == $xml) {
$ex['tblx'] = "Geocode problem with address, revise and retry.";
} elseif("OK" != $xml->status) {
$ex['tblx'] = "Geocode: " . ("ZERO_RESULTS" == $xml->status? "Not Found": substr($xml->status,0,30));
} else{
$dvtby0 = $uca['y0'] = (double)$xml->result->geometry->location->lat; //x
$dvtbx0 = $uca['x0'] = (double)$xml->result->geometry->location->lng;
$dvtby1 = $uca['y1'] = (double)$xml->result->geometry->viewport->southwest->lat; //a
$dvtbx2 = $uca['x1'] = (double)$xml->result->geometry->viewport->southwest->lng;
$dvtbx1 = $uca['x2'] = (double)$xml->result->geometry->viewport->northeast->lng; //b
$dvtby2 = $uca['y2'] = (double)$xml->result->geometry->viewport->northeast->lat;
}//endif
if($ex) {
var_dump($ex);
}
die("did it work?");
geocoder.ca for north america
opencage or geocode.xyz for the world
pelias as standalone
there are lots of options in 2018
The problem here turned out to be the version of PHP. It's really weird and hard to troubleshoot. simplexml_load_file($url) was not returning an object. It wasn't even returning false. The only clue that it was something other that code problem. Upgrade to version 5.6 and it works ok.

Query Remote Windows Service through PHP

The following PHP win32_query_service_status works for querying the local machine, however we are attempting to perform this query on a remote machine within the same domain. We receive no errors however nothing is returned ideas?
$name = "spooler"
$machine = "remotemachine"
$status=win32_query_service_status($name, $machine);
echo "$name service is ";
if ($status["CurrentState"]==1) {echo "Stopped";}
else
if ($status["CurrentState"]==2) {echo "Starting";}
else
if ($status["CurrentState"]==3) {echo "Stopping";}
else
if ($status["CurrentState"]==4) {echo "Running";}

PHP does not resolve .local dns names with gethostbyname

I have werid problem with name resolution. I am trying to connect to active directory server. I can successfully get the ldap server address from the SRV recodrs. Then I try to resolve the dns names to IP addresses and it fails:
<?php
echo 'example.com.:' . PHP_EOL;
echo gethostbyname('example.com.');
echo PHP_EOL;
echo 'dc1.veracomp.local.:' . PHP_EOL;
echo gethostbyname('dc1.my-company.local.');
echo PHP_EOL;
echo 'nslookup dc1.my-company.local.:' . PHP_EOL;
echo `nslookup dc1.my-company.local.`;
The example.com is resolved correctly, then the gethostbyname('dc1.my-company.local.') fails after a few seconds returning dc1.my-company.local. instead of the IP address. Still the same PHP script can call nslookup which correctly resolves the domain name...:
example.com.:
93.184.216.119
dc1.my-company.local.:
dc1.my-company.local.
nslookup dc1.my-company.local.:
Server: xxx.xxx.254.117
Address: xxx.xxx.254.117#53
Name: dc1.my-company.local
Address: 192.168.12.21
What is wrong here?
EDIT:
I am asking for name resolution beacuse the real problem i have is that I can connect to ldap://192.168.12.21 or to ldap://dc1.my-company.pl, but I cannot connect to ldap://dc1.my-company.local.
Unfortunatelly the SRV records for _ldap._tcp.my-company.pl returns only local addresses. I do not want to hardcode the .pl address. And I do not understand why I have to manually resolve the local addresses before passing them to Zend_Ldap as a host option.
You should avoid its use in production. DNS Resolution may take from 0.5 to 4 seconds, and during this time your script is NOT being executed.
I use this one; this will be faster and more efficient:
<?php
function getAddrByHost($hosts, $timeout = 3) {
$returnString = '';
foreach ($hosts as $host) {
$query = `nslookup -timeout=$timeout -retry=1 $host`;
if (preg_match('/\nAddress: (.*)\n/', $query, $matches))
$returnString .= trim($matches[1]) . '<br>';
$returnString .= $host . '<br>';
}
return $returnString;
}
$hostArray[] = 'www.example.com';
$hostArray[] = 'dc1.my-company.local';
$returnString = getAddrByHost($hostArray);
echo $returnString;
?>

php geo ip loc does not work

http://chir.ag/projects/geoiploc/
Hello, I am trying to set this up, but whenever I use include("geoiploc.php"); the page is blank and whenever I remove include("geoiploc.php"); I only see my IP address. I've uploaded geoiploc.php and index.php to a webhost that can run PHP.
If there is ANY other easier way to show country name by IP or any other way, which is it? Please I need this fast
So as I said, it only shows my IP whenever I remove include("geoiploc.php"); why?
You need this library: http://chir.ag/projects/geoiploc/autogen/geoiploc.tar.gz and this is the code to run the script:
include("geoiploc.php"); // Must include this
// ip must be of the form "192.168.1.100"
// you may load this from a database
$ip = $_SERVER["REMOTE_ADDR"];
echo "Your IP Address is: " . $ip . "<br />";
echo "Your Country is: ";
// returns country code by default
echo getCountryFromIP($ip);
echo "<br />\n";
// optionally, you can specify the return type
// type can be "code" (default), "abbr", "name"
echo "Your Country Code is: ";
echo getCountryFromIP($ip, "code");
echo "<br />\n";
// print country abbreviation - case insensitive
echo "Your Country Abbreviation is: ";
echo getCountryFromIP($ip, "AbBr");
echo "<br />\n";
// full name of country - spaces are trimmed
echo "Your Country Name is: ";
echo getCountryFromIP($ip, " NamE ");
echo "<br />\n";
You can use the free MaxMind Geo Lite. Download the files here: http://www.maxmind.com/download/geoip/api/php/php-latest.tar.gz
Then download the Geo Country database from here: http://dev.maxmind.com/geoip/legacy/geolite
You can now use it like this:
<?php
include("geoip.inc");
function ipAddress(){
if (!empty($_SERVER['HTTP_CLIENT_IP'])){ //check ip from share internet
$ip=$_SERVER['HTTP_CLIENT_IP'];
} elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])){ // proxy pass ip
$ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
} else {
$ip=$_SERVER['REMOTE_ADDR'];
}
return $ip;
}
$gi = geoip_open("path/to/GeoIP.dat",GEOIP_STANDARD);
echo geoip_country_name_by_addr($gi, ipAddress());
// echo geoip_country_code_by_addr($gi, ipAddress()); <-- country code
geoip_close($gi);
?>
UPDATE: to get the user's city you should download the top link and look for the file called sample_city.php to see some example code. You'll need to download this file: http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz and place it in the same directory as your php file. A quick example would be:
<?php
include("geoipcity.inc");
function ipAddress(){
if (!empty($_SERVER['HTTP_CLIENT_IP'])){ //check ip from share internet
$ip=$_SERVER['HTTP_CLIENT_IP'];
} elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])){ // proxy pass ip
$ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
} else {
$ip=$_SERVER['REMOTE_ADDR'];
}
return $ip;
}
$gi = geoip_open("GeoIPCity.dat",GEOIP_STANDARD);
$record = geoip_record_by_addr($gi,ipAddress());
print $record->country_code . " " . $record->country_code3 . " " . $record->country_name . "\n";
geoip_close($gi);
?>

PHP: inconsistent results for gethostbyname() and checkdnsrr()

I'm writing a simple form to check if a .com domain is available. I'm evaluating 3 methods (#dns_get_record, gethostbyname() and checkdnsrr()) for a bunch of test domain names with the below code, but I am getting all not available entries for gethostbyname() and checkdnsrr(), which you can see # http://suggestmyname.com/nonwp/arraypush.php if you refresh a few times. I don't get this issue when running on my local testing server, anyone know why the inconsistency? Also not all of the domains are being checked on my remote server, but are on my local testing server.
$sdomains = array();
array_push($sdomains, 'etc.com');
function domainAvailable1($domain){
$results = #dns_get_record($domain, DNS_ANY);
return empty($results);
}
function domainAvailable2($domain){
return gethostbyname($domain) == $domain;
}
function domainAvailable3($domain){
return !checkdnsrr($domain, 'ANY');
}
echo '<table><tr><td>#dns_get_record</td><td>gethostbyname</td><td>checkdnsrr</td></tr>';
foreach($sdomains as $sdomain){
if (domainAvailable1($sdomain) == true){
echo '<tr><td bgcolor=green>' . $sdomain . ' is available!</td>';
} else {
echo '<tr><td bgcolor=red>' . $sdomain . ' is NOT available.</td>';
}
if (domainAvailable2($sdomain) == true){
echo '<td bgcolor=green>' . $sdomain . ' is available!</td>';
} else {
echo '<td bgcolor=red>' . $sdomain . ' is NOT available.</td>';
}
if (domainAvailable3($sdomain) == true){
echo '<td bgcolor=green>' . $sdomain . ' is available!</td></tr>';
} else {
echo '<td bgcolor=red>' . $sdomain . ' is NOT available.</td></tr>';
}
}
I'm not sure that using any of those functions (basically using DNS in general) is a reliable method to determine if a domain is available.
I have plenty of domains registered that have absolutely no DNS records so they can't be resolved by any means, but the domains are of course taken.
While most domains will have some DNS records, there will be cases where there aren't any. For example the domain mymoney.com in your script says it is available through all 3 checks, but the domain is registered.
Your best bet is to use WHOIS to query the appropriate registrar for a given TLD and use WHOIS to determine availability. I think if you try to use DNS, there will be false positives or other problems you will run into.

Categories