Home > database >  Using PHP variable in dns_get_record?
Using PHP variable in dns_get_record?

Time:10-19

I'm getting an error with this code:

$type = "DNS_" . strtoupper($_GET['type']);
$array = dns_get_record($domain, $type);

Helpful info:

$_GET['type'] = "a"

If I enter DNS_A in dns_get_record, the code works. But when entering $type, I get an error.

The type is dynamic and I get it from a URL parameter, which is why I can't hard code it in.

What am I doing wrong?

CodePudding user response:

Parameter 2 has to be one of these predefined constant

DNS_A, DNS_CNAME, DNS_HINFO, DNS_CAA, DNS_MX, DNS_NS, DNS_PTR, DNS_SOA,
DNS_TXT, DNS_AAAA, DNS_SRV, DNS_NAPTR, DNS_A6, DNS_ALL or DNS_ANY

so you will need to get the value of the constant in order to make your code flexible like that.

You were passing a string so use constant() to convert that string into the integer value associated with that constant

Example

$_GET['type'] = 'a';

$type = constant("DNS_" . strtoupper($_GET['type']));
$domain = 'www.php.net';

$result = dns_get_record($domain, $type);
print_r($result);

RESULT

Array
(
    [0] => Array
        (
            [host] => www-php-net.ax4z.com
            [class] => IN
            [ttl] => 283
            [type] => A
            [ip] => 185.85.0.29
        )
)

CodePudding user response:

dns_get_record expects second parameter to be Integer. In yoru case it is a string.

Please replace following numbers based on your required records:

$dns_record = dns_get_record('domain', REPLACE_RECORD_ID);
var_dump($dns_record);


DNS_A = 1
DNS_NS = 2
DNS_CNAME = 16
DNS_SOA = 32
DNS_PTR = 2048
DNS_HINFO = 4096
DNS_MX = 16384
DNS_TXT = 32768

List of DNS Record Types

CodePudding user response:

DNS_A actually means integer 1, NOT a string "DNS_A"

So please use

<?php
$type=1; 
$result = dns_get_record("php.net", $type );
print_r($result);
?>

or use $_GET['type'] = 1 and then use

<?php
$type=$_GET["type"]; 
$result = dns_get_record("php.net", $type );
print_r($result);
?>

Please see official documentation: ( int $type = DNS_ANY .... and so on)

https://www.php.net/manual/en/function.dns-get-record.php

Note: you can mix the $type like:

<?php
$dnsr = dns_get_record('php.net', DNS_A   DNS_NS);
// same as $dnsr = dns_get_record('php.net', 3);
print_r($dnsr);
?>

Keys (pre-defined constants)

DNS_A = 1
DNS_NS = 2
DNS_CNAME = 16
DNS_SOA = 32
DNS_PTR = 2048
DNS_HINFO = 4096
DNS_MX = 16384
DNS_TXT = 32768
DNS_A6 = 16777216
DNS_SRV = 33554432
DNS_NAPTR = 67108864
DNS_AAAA = 134217728
DNS_ALL = 251713587
DNS_ANY = 268435456
  • Related