DNS_GET_RECORD MX lookup failing - php

I have a PHP script that is using get_dns_record to retrieve and display specific DNS records for a domain, submitted via a form.
It's working really well, except that the section that handles MX records is a little unreliable. Sometimes no MX Records are displayed at all (on domains I know have them). If you refresh 2-3 times, sometimes they will show up. Sometimes they won't.
Thoughts?
function getDNSRecord($domain1) {
$dns = dns_get_record( $domain1, DNS_ANY );
echo "These are DNS records";
foreach( $dns as $d ) {
// Only print A and MX records
if( $d['type'] != "A" and $d['type'] != "MX" )
continue;
// Print type specific fields
switch( $d['type'] ) {
case 'A':
// Display annoying message
echo "<b>\n" . $d['ip'] . "</b>\n is the Primary A Record for this domain.";
break;
case 'MX':
// Resolve IP address of the mail server
$mx = dns_get_record( $d['target'], DNS_A );
foreach( $mx as $server ) {
echo "This MX record for " . $d['host'] . " points to the server <b>\n" . $d['target'] . "</b>\n whose IP address is <b>\n" . $server['ip'] . "</b>. It has a priority of <b>\n" . $d['pri'] . "</b>\n.";
}
if ( $d['target'] == $domain1 ) {
echo "<div id='mx-status'>There is an issue with this MX Record</div>\n";
} else {
echo "<div id='mx-status'>This MX Record looks fine.</div>\n";
}
break;
}
}
}

Have you considered using getmxrr() to get the mx records for the domain? Documentation here: http://us2.php.net/manual/en/function.getmxrr.php

Related

unique hit counter using file handling not working properly?

There are three files: index.php, ip.txt that contains ip address which are allowed to visit site and count.txt which simply counts the hit if any user visits the site with specified ip addresses in ip.txt. But the problem is when ip.txt contains only my laptops ip local machines ip address, it works perfectly and increases the ip address by one. But when ip.txt contains many ip address along with my local machines ip address if block in foreach loop checks them and and doesn't increase the count. However it should increase the count as my ip address is also there in ip.txt file. Please help!!
index.php:
<?php
$ip_address = $_SERVER['REMOTE_ADDR'];
$handle = fopen('ip.txt','r');
$file_read = fread($handle, filesize('ip.txt'));
$file_read_array = explode('\n',$file_read);
foreach($file_read_array as $ip)
{
if(trim($ip) == trim($ip_address))
{
$visit =true;
echo 'true';
break;
}
else
{
$visit =false;
echo 'false';
}
}
if($visit == true)
{
$handle_1 = fopen('count.txt','r');
$file_read_1 = fread($handle_1, filesize('count.txt'));
$increment = $file_read_1 + 1;
fclose($handle_1);
$handle_2 = fopen('count.txt','w');
fwrite($handle_2,$increment);
fclose($handle_2);
}
else
{
echo 'ip address not found.';
}
?>
ip.txt:
127.0.0.1
100.100.100.100
102.1.1.1
count.txt:
0

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.

PHP to return gethostbyname($lookup) value

I am using the code below (simplified version) to determine if my IPs are on a blacklist. I need to modify it to be able to determine if an IP is on a Whitelist. The function will require me to see a specific code returned.
127.0.0.1
127.0.0.2
127.0.0.3
127.0.0.4
127.0.0.5
How can this be adjusted to return the (code) output value when the script runs?
$host = '222.22.222.222';
$rbl = 'hostkarma.junkemailfilter.com';
$rev = array_reverse(explode('.', $host));
$lookup = implode('.', $rev) . '.' . $rbl;
if ($lookup != gethostbyname($lookup)) {
echo "ip: $host is listed in $rbl\n";
} else {
echo "ip: $host NOT listed in $rbl\n";
}
EDIT: Sorry guys, The function of the script above will return confirmation if the IP address is on the blacklist entered in $rlb. However, Hostkarma returns a code, one of the 127.0 codes shown above as each code indicates a different block status. I need to get the code. "echo $lookup;" just returns the reverse lookup, like this: 222.222.22.222.hostkarma.junkemailfilter.com
$lookup = implode('.', $rev) . '.' . $rbl;
$value = gethostbyname($lookup);
if ($lookup != $value){
echo "ip: $host is listed in $rbl\n";
echo "return value: $value\n";
}
else{
echo "ip: $host NOT listed in $rbl\n";
}
The 127.x.x.x code should be given to you as the value returned by gethostbyname.
Do you mean this?
echo $lookup;

Categories